In Python, we can specify the number of runs or loops when using %timeit in Jupyter notebooks or the timeit module in regular Python scripts.
Here's how to specify the number of runs/loops using %timeit:
%timeit -r 5 -n 100 my_function() |
This will run the function my_function() 100 times per loop, and repeat the loop 5 times. The -r parameter specifies the number of loops, and the -n parameter specifies the number of runs per loop.
And here's how to specify the number of runs/loops using timeit.timeit:
import timeitdef my_function(): x = 1 + 2print(timeit.timeit(my_function, number=100000, repeat=5)) |
This will run the function my_function() 100,000 times, and repeat the measurement 5 times. The number parameter specifies the number of times to execute the code in each measurement, and the repeat parameter specifies the number of times to repeat the measurement.
By specifying the number of runs/loops, we can get more accurate timing results and reduce the variability of the measurements. This is especially important for small code snippets that can be affected by variations in system load or other factors.