timeit
is more accurate, for three reasons:
time.time
or time.clock
in Python 2 and time.perf_counter()
on Python 3. See timeit.default_timer
.At any given time, the Central Processing Unit (CPU) is used and shared by many processes. Measurements taken using time.time
are relative to what we call wall clock. This means that the results are dependent to the other processes that were running at the time the test was executed. Therefore, in many cases the results produced by time.time
are not as accurate as possible.
More reliable results can be generated using time.clock
for Python 2.x and time.process_time()
or time.perf_counter()
for Python 3.X, that measures the CPU cycles used during the execution of the code but even this method as it heavily relies on the specific machine you are executing the tests. For example, the results might hugely differ if the tests are executed on different machines (even though the algorithm and input data are both exactly the same)
timeit.timeit
is a an advanced library that is more accurate and reliable compared to time.time
and time.clock
as it takes into account the factors that pose the variance among the code executions and trials, by simply repeating the execution of tests in order to produce more reliable and accurate results.+
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