Search code examples
pythonbenchmarkingtiming

timeit.repeat much faster than tic-toc even for one operation?


I have a simple function fun and I'd like to measure how long it takes and also get the value back. I thought Python's "tic-toc" is the way to go, but I noticed that it's significanly slower than timeit (which doesn't return the value):

import numpy as np
import time
import timeit

np.random.seed(0)
x = np.random.rand(10000)

def fun():
    return np.sqrt(x)


t0_ns = time.time_ns()
fun()
t1_ns = time.time_ns()
print(t1_ns - t0_ns)


out = timeit.repeat(
  stmt=fun,
  repeat=1,
  number=1,
  timer=time.perf_counter_ns,
)
print(out[0])
58002
27037

In fact, timeit.repeat is more than twice as fast as the function evaluation itself! What's the reason for this? Is there a way around it?


Solution

  • Promoting Peter Cordes' reply to an answer here.

    When reverting the order,

    import numpy as np
    import time
    import timeit
    
    np.random.seed(0)
    x = np.random.rand(10000)
    
    
    def fun():
        return np.sqrt(x)
    
    
    out = timeit.repeat(
        stmt=fun,
        repeat=1,
        number=1,
        timer=time.perf_counter_ns,
    )
    print(out[0])
    
    t0_ns = time.time_ns()
    fun()
    t1_ns = time.time_ns()
    print(t1_ns - t0_ns)
    
    36709
    12623
    

    the second operation is faster again. This makes clear that the execution speed has nothing to do with the timing methods, but with CPU warm-up effects.