time.time vs. timeit.timeit

PythonPerformanceTimePython 3.x

Python Problem Overview


Sometimes, I like to time how long it takes parts of my code to run. I've checked a lot of online sites and have seen, at large, two main ways to do this. One is using time.time and the other is using timeit.timeit.

So, I wrote a very simple script to compare the two:

from timeit import timeit
from time import time
start = time()
for i in range(100): print('ABC')
print(time()-start, timeit("for i in range(100): print('ABC')", number=1))

Basically, it times how long it takes to print "ABC" 100 times in a for-loop. The number on the left is the results for time.time and the number on the right is for timeit.timeit:

# First run
0.0 0.012654680972022981
# Second run
0.031000137329101562 0.012747430190149865
# Another run
0.0 0.011262325239660349
# Another run
0.016000032424926758 0.012740166697164025
# Another run
0.016000032424926758 0.0440628627381413

As you can see, sometimes, time.time is faster and sometimes it's slower. Which is the better way (more accurate)?

Python Solutions


Solution 1 - Python

timeit is more accurate, for three reasons:

  • it repeats the tests many times to eliminate the influence of other tasks on your machine, such as disk flushing and OS scheduling.
  • it disables the garbage collector to prevent that process from skewing the results by scheduling a collection run at an inopportune moment.
  • it picks the most accurate timer for your OS, time.time or time.clock in Python 2 and time.perf_counter() on Python 3. See timeit.default_timer.

Solution 2 - Python

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.+

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2555451View Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow
Solution 2 - PythonGiorgos MyrianthousView Answer on Stackoverflow