I modified some code I found here on stackoverflow, but I get some weird behaviour. Can anyone see my error?
import atexit
from time import clock
line = "="*10 + ">>>"
def secondsToStr(s):
return "{0}.{1}ms".format(round(s*1000), round(s*1000000))
##
# @brief Starts a timer. If the program crashes, the end function will be called anyway.
def start():
atexit.register(stop)
print(line,"Start time measurement")
startTime = clock()
print("new start time:", startTime)
##
# @brief Needs to be called after start(). Prints the time passed since start() was called.
def stop():
end = clock()
elapsed = end-startTime
print(startTime, end) # Inserted for debugging
print(line, "Ellapsed time:", secondsToStr(elapsed))
atexit.unregister(stop)
def now():
return secondsToStr(clock())
startTime = clock()
The idea is to call start()
and stop()
to measure the time the program takes in between those two calles. Weirdly, the startTime
does not change. Thus every call of stop()
gives the past time since the first call of start()
. Here is an example output:
==========>>> Start time measurement
new start time: 0.285078
Doing work
0.231932 1.766478
==========>>> Ellapsed time: 1535.1534546ms
==========>>> Start time measurement
new start time: 1.766624
More work
0.231932 1.975752
==========>>> Ellapsed time: 1744.1743820ms
==========>>> Start time measurement
new start time: 1.975821
More work
0.231932 1.976301
==========>>> Ellapsed time: 1744.1744369ms
Why is the startTime
never changing? It should get a new value every time start()
is called.