Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?
for example:
start = time.clock()
... do something
elapsed = (time.clock() - start)
vs.
start = time.time()
... do something
elapsed = (time.time() - start)
I use this code to compare 2 methods .My OS is windows 8 , processor core i5 , RAM 4GB
output:
time() = 0.0993799996376
clock() = 0.0993572257367
To the best of my understanding, time.clock() has as much precision as your system will allow it.
As of 3.3, time.clock() is deprecated, and it's suggested to use time.process_time() or time.perf_counter() instead.
Previously in 2.7, according to the time module docs:
Additionally, there is the timeit module for benchmarking code snippets.
Right answer : They're both the same length of a fraction.
But which faster if
subject
istime
?A little test case :
I am not work an Swiss labs but I've tested..
Based of this question :
time.clock()
is better thantime.time()
Edit :
time.clock()
is internal counter so can't use outside, got limitationsmax 32BIT FLOAT
, can't continued counting if not store first/last values. Can't merge another one counter...One thing to keep in mind: Changing the system time affects
time.time()
but nottime.clock()
.I needed to control some automatic tests executions. If one step of the test case took more than a given amount of time, that TC was aborted to go on with the next one.
But sometimes a step needed to change the system time (to check the scheduler module of the application under test), so after setting the system time a few hours in the future, the TC timeout expired and the test case was aborted. I had to switch from
time.time()
totime.clock()
to handle this properly.