python get_clock_info 函数
get_clock_info函数获取时钟的基本信息,得到的值因不同系统存在差异,函数原型比较简单:
time.get_clock_info(name)
其中 name 可以取下述值:
monotonic:time.monotonic()
perf_counter: time.perf_counter()
process_time: time.process_time()
thread_time: time.thread_time()
time: time.time()
该函数的返回值具有以下属性:
adjustable : 返回 True 或者 False。如果时钟可以自动更改(例如通过 NTP 守护程序)或由系统管理员手动更改,则为 True ,否则为 False ;
implementation : 用于获取时钟值的基础 C 函数的名称,就是调用底层 C 的函数;
monotonic :如果时钟不能倒退,则为 True ,否则为 False;
resolution : 以秒为单位的时钟分辨率( float )。
import time available_clocks = [ ('clock', time.clock), ('monotonic', time.monotonic), ('perf_counter', time.perf_counter), ('process_time', time.process_time), ('time', time.time), ] for clock_name, func in available_clocks: print(''' {name}: adjustable : {info.adjustable} implementation: {info.implementation} monotonic : {info.monotonic} resolution : {info.resolution} current : {current} '''.format( name=clock_name, info=time.get_clock_info(clock_name), current=func()))
上图显示橡皮擦的计算机在 clock
与 perf_counter
中,调用底层 C 函数是一致的。