rougier / from-python-to-numpy

An open-access book on numpy vectorization techniques, Nicolas P. Rougier, 2017
http://www.labri.fr/perso/nrougier/from-python-to-numpy
Other
2.03k stars 339 forks source link

2.1 Introduction random walk example #81

Closed foni closed 5 years ago

foni commented 5 years ago

Hi, I cannot wrap my head around the big difference between the execution times when I use timeit() from tools.py versus the ipython magic function %timeit. can someone help me?

Regards, fonis.

%timeit "random_walk_fastest(n=10000)" 7.52 ns ± 0.258 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

timeit("random_walk_fastest(n=10000)", globals()) 1000 loops, best of 3: 99 usec per loop

ashwinvis commented 5 years ago

WhatYou don't need the quotes while calling the magic. Try:

%timeit random_walk_fastest(n=10000)
foni commented 5 years ago

Thanks a lot. Without the quotes, the measurements are very close.

So with quotes, %timeit was just evaluating the string literal expression "random_walk_fastest(n=10000)" instead of calling random_walk_fastest() function.