I am looking for a way to measure the CPU time of the part of my python program. I was looking around and it seems all the time measuring functions that I could find measure the wall clock time on windows. Is there a way to measure the CPU time on windows for python ? Thank you.
time.process_time()
https://www.python.org/dev/peps/pep-0418/
The new time.process_time() function acts as a portable counter that always measures CPU time (excluding time elapsed during sleep) and has the best available resolution.
Earlier answer for anyone wanting to use timeit() to average:
I wrote this while recently learning Python. It's a timeit.Timer class iterating timeit() over functions for averaging; in my research timing seemed to be easy to mess up and timeit() apparently tricky to get working (though easier to get working on command line). This works.
Also note this answer suggests the best bet is time.process_time() for Python 3.3+ and reminds that .default_timer() is wall time:
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)))
Output
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With