繁体   English   中英

以 CPU 周期测量执行时间?

[英]Measure execution time in CPU cycles?

我的笔记本电脑花了 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) 

它应该有帮助。

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}')

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM