About magic command %%timeit -n1 -r10

pls elaborate to what does it mean by “-n1” and “-r10” in the magic command “%%timeit -n1 -r10”?
As I have seen the lectures but I am not able to understand about it.

and one more doubt, when I run the same program again and again I got different time, Why?

Thanks:)

You can check the docs to understand the functionality of those parameters:
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit

Basically, timeit is a magic function to extensively test a given block of code.
The argument -n 1 means that run the given block of code only once, and -r 10 means that repeat this experiment of measuring time 10 times and get the best result out of it.

Each time you run the command, the time will differ slightly. It’s mainly because of how your OS handles multi-processing, that is, how it allocates/prioritizes time chunks for each process running under the OS.

1 Like

okk, So my doubt is when you repeat the experiment of measuring time 10 times then you also have to run your code 10 times?
Or when you run any particular block of code once then you can measure the time also once?
Pls clarify it…

No, you don’t have to do that manually.
The magic command will take care of running it 10 times for you and it’ll give the best result.

1 Like

The magic command will run the code by itself for 10 times when you pass %%timeit -n1 -r10 for a particular cell

1 Like