【发布时间】:2017-04-09 07:00:18
【问题描述】:
我正在寻找一种方法来测量我的 python 程序部分的 CPU 时间。我环顾四周,似乎一直在测量我能找到的测量窗户上挂钟时间的功能。有没有办法在 windows 上为 python 测量 CPU 时间?谢谢。
【问题讨论】:
标签: python python-3.x time
我正在寻找一种方法来测量我的 python 程序部分的 CPU 时间。我环顾四周,似乎一直在测量我能找到的测量窗户上挂钟时间的功能。有没有办法在 windows 上为 python 测量 CPU 时间?谢谢。
【问题讨论】:
标签: python python-3.x time
time.process_time()
https://www.python.org/dev/peps/pep-0418/
新的 time.process_time() 函数充当便携式计数器, 总是测量 CPU 时间(不包括睡眠期间经过的时间)并且有 最佳可用分辨率。
较早的答案适用于任何想要使用 timeit() 来平均的人:
我最近在学习 Python 时写了这篇文章。这是一个 timeit.Timer 类迭代 timeit() 函数进行平均;在我的研究中,时间似乎很容易搞砸,而 timeit() 显然很难开始工作(尽管更容易在命令行上工作)。这行得通。
另请注意,此答案表明对于 Python 3.3+,最好的选择是 time.process_time(),并提醒 .default_timer() 是挂钟时间:
https://stackoverflow.com/a/15176730/3981745
""" Sum a range with timing/comparisons.
- Manually summing from low..high : a beginner's loop with no algorithms experience
- Python's sum() - does the same thing for an "efficient" programmer
- Fast sum - a programmer with math focus
- Timing calls (easy to do wrong)
This is a trivial formula to demonstrate what a little math can do; for games this type of optimization is critical. It could probably be even more optimized.
"""
def slow_way(lo, hi):
s=0
for i in range(lo, hi+1):
s+=i
return s
def fast_way(lo, hi):
lph=lo+hi # lo plus hi
hmlpo=hi-lo+1 # hi minus lo plus one
pairs=hmlpo//2
#print("-", lo,hi,pairs, pairs*lph, (lo+pairs)*(hmlpo%2))
return (pairs*lph + (lo+pairs)*(hmlpo%2))
import timeit
# 'builtin' hack doesn't seem to work
#import __builtin__
#__builtin__.__dict__.update(locals())
ranges=[]
ranges.append((1,10,))
ranges.append((2,10,))
ranges.append((3,10,))
ranges.append((4,10,))
ranges.append((5,10,))
ranges.append((32,10032,))
print("Calculation check...")
print("slow : sum : fast : should all match")
for tupl in ranges:
l=tupl[0]; h=tupl[1]
print("{} : {} : {}".format(slow_way(l,h), sum(range(l, h+1)), fast_way(l,h)))
iters=100000
print("-"*20 +"\nTime compare ({} iterations) : lower is better".format(iters))
slow=timeit.Timer("slow_way(1,101)", "from __main__ import slow_way")
print("slow: {0:.6f}".format(slow.timeit(number=iters)))
# functions include last number, range should be +1
s=timeit.Timer("sum(range(1,102))", "")
print(" sum: {0:.6f}".format(s.timeit(number=iters)))
fast=timeit.Timer("fast_way(1,101)", "from __main__ import fast_way")
print(" fast: {0:.6f}".format(fast.timeit(number=iters)))
输出
Calculation check...
slow : sum : fast : should all match
55 : 55 : 55
54 : 54 : 54
52 : 52 : 52
49 : 49 : 49
45 : 45 : 45
50325032 : 50325032 : 50325032
--------------------
Time compare (100000 iterations) : lower is better
slow: 4.719885
sum: 0.963886
fast: 0.343524
【讨论】:
time.clock()
在 Windows 上,您可以使用 time.clock(),它基本上使用来自 Windows API 的 QueryPerformanceCounter()。
查询性能计数器():
检索性能计数器的当前值,这是一个高分辨率 (
这里是来自 python 文档的 time.clock 的详细解释:
在 Unix 上,将当前处理器时间作为浮点数返回,以秒为单位。精度,实际上是“处理器时间”含义的定义,取决于同名 C 函数的精度,但无论如何,这是用于对 Python 或计时算法进行基准测试的函数。 在 Windows 上,此函数根据 Win32 函数 QueryPerformanceCounter() 返回自第一次调用此函数以来经过的挂钟秒数,作为浮点数。分辨率通常优于 1 微秒。
时间
如果您需要更准确的信息,请访问 timeit。这个答案解释了timeit的好处:https://stackoverflow.com/a/17579466/2394967
【讨论】:
QueryPerformanceCounter 又是一个高分辨率的墙上时钟