Pythom time方法clock()在unix上返回以秒表示的浮点数形式的当前处理器时间。精确度取决于同名C函数的精确度,但在任何情况下,这都是用于对Python或计时算法进行基准测试的函数。
在Windows上,此函数根据Win32函数QueryPerformanceCounter以浮点数的形式返回自第一次调用此函数以来经过的挂钟秒数。
time.clock()
此方法在Unix上将当前处理器时间返回为以秒为单位的浮点数,而在Windows中,它将自第一次调用此函数以来经过的挂钟秒数作为浮点数返回。
以下示例显示了clock()方法的用法。
链接:https://www.learnfk.comhttps://www.learnfk.com/python/time-clock.html
来源:LearnFk无涯教程网
#!/usr/bin/python import time def procedure(): time.sleep(2.5) # measure process time t0=time.clock() procedure() print time.clock(), "seconds process time" # measure wall time t0=time.time() procedure() print time.time() - t0, "seconds wall time"
当无涯教程运行上面的程序时,它产生以下输出-
0.0 seconds process time 2.50023603439 seconds wall time
注意-并不是所有系统都可以测量真正的处理时间。在这样的系统(包括Windows)上,CLOCK通常测量自程序启动以来的墙壁时间。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)