In Jupyter notebooks, %timeit can be used in both line magic mode and cell magic mode.
In line magic mode, %timeit can be used to time a single line of code, like this:
%timeit x = 1 + 2 |
This will time how long it takes to execute the statement x = 1 + 2 and print the results.
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 10 x = 1 + 2 |
This will run the statement x = 1 + 2 100 times per loop, and repeat the loop 10 times. The results will be printed to the console.
In general, line magic mode is useful for timing small code snippets or individual statements, while cell magic mode is useful for timing larger blocks of code or entire functions.