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
ortime.clock
in Python 2 andtime.perf_counter()
on Python 3. Seetimeit.default_timer
.