Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time.time vs. timeit.timeit

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.

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