title: Measure runtime of a Jupyter Notebook code cell
body: <p>It seems that in Spyder (IPython3 Kernel) one can easily time a code cell by running the <code>%%time</code> or <code>%%timeit</code> command at the top of the code cell:</p>
  
  <pre><code>#%%
  %%time # or %%timeit which measures average runtime from multiple runs
  ....
  
  #%% (the previous cell ends and the next begins)
  </code></pre>
  
  <p>Running the above code can get the runtime of the cell defined by the pair of <code>#%%</code>. This is how things work in Spyder, but doesn't quite work in the Jupyter Notebook environment.</p>
  
  <p>In Jupyter code cells aren't defined by <code>#%%</code> delimiters, rather they are created by clicking a button in the menu bar. And as far as I tried, the command <code>%%time</code> and <code>%%timeit</code> both raise compilation error. It seems that Jupyter can't recognise them, but it's strange because my Jupyter actually uses the same IPython kernel as Spyder does. One thing that works in Jupyter is the <code>%time</code> and <code>%timeit</code> commands, but they can only measure the runtime of a one-line code, i.e., must be formulated like</p>
  
  <pre><code>%time blah blah
  </code></pre>
  
  <p>and it turns out I can't even measure a <code>for</code> loop which consists of more than one line. So this method is not desirable for me. Is there just any way to evaluate a cell runtime using the magic command <code>%time(it)</code> and the like in Jupyter?</p>
  
  <p>(PS: If as in Spyder I attach a <code>%time</code> command at the top of a cell it will give <code>Wall time: 0 ns</code> because there is nothing following it <em>in that same line</em> and it actually measures nothing.)</p>