In Jupyter notebooks, %timeit can also be used in cell magic mode to time larger blocks of code or entire functions.
To use %timeit in cell magic mode, we simply add %%timeit at the beginning of the cell, like this:
%%timeitfor i in range(1000): x = i**2 + i**3 |
This will time how long it takes to execute the entire cell, which in this case is a for loop that computes some values.
By default, %timeit will run the code several times to get an accurate measurement. The number of loops and runs can be adjusted using the -n and -r options, like this:
%%timeit -n 100 -r 10for i in range(1000): x = i**2 + i**3 |
This will run the for loop 100 times per loop, and repeat the loop 10 times. The results will be printed to the console.
In general, cell magic mode is useful for timing larger blocks of code or entire functions, while line magic mode is useful for timing small code snippets or individual statements.