python实训计算总秒数,Python:如何获取每个吉利秒数
I'd like to know the HZ of the system, i.e. how many mili seconds is one jiffy from Python code.
解決方案
There is USER_HZ
>>> import os
>>> os.sysconf_names['SC_CLK_TCK']
2
>>> os.sysconf(2)
100
which is what the kernel uses to report time in /proc.
From the time(7) manual page:
The Software Clock, HZ, and Jiffies
The accuracy of various system calls that set timeouts, (e.g.,
select(2), sigtimedwait(2)) and measure CPU time (e.g., getrusage(2))
is limited by the resolution of the software clock, a clock maintained
by the kernel which measures time in jiffies. The size of a jiffy is
determined by the value of the kernel constant HZ.
The value of HZ varies across kernel versions and hardware platforms.
On i386 the situation is as follows: on kernels up to and including
2.4.x, HZ was 100, giving a jiffy value of 0.01 seconds; starting with
2.6.0, HZ was raised to 1000, giving a jiffy of 0.001 seconds. Since
kernel 2.6.13, the HZ value is a kernel configuration parameter and can
be 100, 250 (the default) or 1000, yielding a jiffies value of, respec‐
tively, 0.01, 0.004, or 0.001 seconds. Since kernel 2.6.20, a further
frequency is available: 300, a number that divides evenly for the com‐
mon video frame rates (PAL, 25 HZ; NTSC, 30 HZ).
The times(2) system call is a special case. It reports times with a
granularity defined by the kernel constant USER_HZ. Userspace applica‐
tions can determine the value of this constant using
sysconf(_SC_CLK_TCK).
If you absolutely must know SYSTEM_HZ:
>>> from ctypes import *
>>> rt = CDLL('librt.so')
>>> CLOCK_REALTIME = 0
>>> class timespec(Structure):
... _fields_ = [("tv_sec", c_long), ("tv_nsec", c_long)]
...
>>> res = timespec()
>>> rt.clock_getres(CLOCK_REALTIME, byref(res))
0
>>> res.tv_sec, res.tv_nsec
(0, 4000250)
>>> SYSTEM_HZ = round(1/(res.tv_sec + (res.tv_nsec/10.0**9)))
Gives 250 on my laptop (which sounds about right) and 1000000000 in a VM…
總結
以上是生活随笔為你收集整理的python实训计算总秒数,Python:如何获取每个吉利秒数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数学不好的人可以学python吗_哪些人
- 下一篇: python如何计算整数和_Python