Tho*_*mas 55 python linux benchmarking clock
我想记录真实壁挂时间有多长.目前我这样做:
startTime = time.time()
someSQLOrSomething()
print "That took %.3f seconds" % (time.time() - startTime)
Run Code Online (Sandbox Code Playgroud)
但是如果在SQL查询(或其他任何东西)运行时调整时间,则会失败(产生不正确的结果).
我不想只是对它进行基准测试.我想在实时应用程序中记录它,以便查看实时系统的趋势.
我想要像clock_gettime(CLOCK_MONOTONIC,...)这样的东西,但是在Python中.并且最好不必编写调用clock_gettime()的C模块.
Arm*_*her 77
该函数非常简单,您可以使用ctypes来访问它:
#!/usr/bin/env python
__all__ = ["monotonic_time"]
import ctypes, os
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
class timespec(ctypes.Structure):
_fields_ = [
('tv_sec', ctypes.c_long),
('tv_nsec', ctypes.c_long)
]
librt = ctypes.CDLL('librt.so.1', use_errno=True)
clock_gettime = librt.clock_gettime
clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def monotonic_time():
t = timespec()
if clock_gettime(CLOCK_MONOTONIC_RAW , ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t.tv_sec + t.tv_nsec * 1e-9
if __name__ == "__main__":
print monotonic_time()
Run Code Online (Sandbox Code Playgroud)
Pad*_*118 37
现在,在Python 3.3中你将使用time.monotonic.
| 归档时间: |
|
| 查看次数: |
23054 次 |
| 最近记录: |