【问题标题】:Measure execution time in CPU cycles?以 CPU 周期测量执行时间?
【发布时间】:2017-01-14 17:42:29
【问题描述】:

我的笔记本电脑花了 1.3 秒来完成下面发布的过程。 相同的代码在另一台机器上运行,时间不同:2.1 秒。这是因为另一台机器运行在不同的操作系统上,它具有不同的 CPU、内存等。

我想知道是否有一种方法可以测量计算机完成给定进程所花费的 CPU 周期,而不是以秒为单位计时。因此,如果相同的代码在不同的机器上运行,所进行的测量总是会产生相同的数字,结果将类似于:it took 10,000 CPU cycles for this process to complete....

import time
def run():
    for i in range(10000000):
        0+0
start_time = time.time()
run()
print 'processed in: %s sec'%(time.time() - start_time) 

【问题讨论】:

  • 您可能希望将标题更改为更具体。因为现在,这听起来像是常见的重复。

标签: python time operating-system


【解决方案1】:

应该有帮助。

from hwcounter import Timer, count, count_end
from time import sleep
from math import sqrt

start = count()
sqrt(144) / 12
elapsed = count_end() - start
print(f'elapsed cycles: {elapsed}')

【讨论】:

  • 有没有办法在 Windows 中做到这一点?
【解决方案2】:

在 linux 系统上你有time 命令,即:

# time ls
/bin/ls ${=LS_OPTIONS}  0.00s user 0.00s system 68% cpu 0.003 total

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多