title: How to use %%timeit cell magic and exclude setup code?
body: <p>The <a href="http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit" rel="noreferrer"><code>%timeit</code></a> magic supports execution in line mode and cell mode.  Using the cell mode, invoked with <code>%%timeit</code> (note: two percent symbols), can be used to exclude some setup code from the measurement:</p>
  
  <pre><code>%%timeit [-n&lt;N&gt; -r&lt;R&gt; [-t|-c] -q -p&lt;P&gt; -o] setup_code code code...
  </code></pre>
  
  <p>But how do you use it?  This gives an error:</p>
  
  <pre><code>&gt;&gt;&gt; %%timeit sleep(0.1); sleep(0.1)
  ... 
  UsageError: %%timeit is a cell magic, but the cell body is empty. 
  Did you mean the line magic %timeit (single %)?
  </code></pre>
  
  <p>And this doesn't exclude the first line from the benchmark:</p>
  
  <pre><code>&gt;&gt;&gt; %%timeit
  ... sleep(0.1)
  ... sleep(0.1)
  ... 
  200 ms ± 17.6 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
  </code></pre>