Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between %time vs %%time vs %timeit %%timeit in Jupyter Notebook

Help me in finding out the exact difference between time and timeit magic commands in jupyter notebook.

%time

a="stack overflow"
time.sleep(2)
print(a)

It shows only some micro seconds, But I added sleep time as 2 seconds

like image 341
Dhamodharan R Avatar asked Dec 08 '25 08:12

Dhamodharan R


1 Answers

Magics that are prefixed with double percentage signs (%%) are considered cell magics. This differs from line magics which are prefixed with a single percentage sign (%).

It follows that,

  • time is used to time the execution of code either at a line level (%) or cell level (%%).
  • timeit is similar but runs the code multiple times to provide more accurate and statistically significant timing information.

The key distinction is that %time and %%time (docs) measure the execution time of code once, while %timeit and %%timeit (docs) measure the execution time of code multiple times to give an average time and standard deviation.

Hence,

%%time

a="stack overflow"
time.sleep(2)
print(a)

This output shows both the CPU time and the wall time. The wall time includes the 2 seconds sleep time, while the CPU time accounts for the actual processing time.

Replacing %%time with %time will show the CPU time and the wall time for the next single line that is executed.

like image 52
jjvraw Avatar answered Dec 09 '25 21:12

jjvraw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!