title: How to run an IPython magic from a script (or timing a Python script)
body: <p>The IPython %timeit magic command does its job well for measuring time required to run some Python code. Now, I want to use something analogous in the Python script. I know about the timeit module, however, it has several disadvantages, for example, how to select the number of runs adaptively? i.e., the default code</p>
  
  <pre><code>import timeit
  t=timeit.Timer("code(f)", "from __main__ import code,f")
  t.timeit() 
  </code></pre>
  
  <p>runs the code million times. The %timeit IPyhton magic command does it automatically. I suggest that I could use something like the MATLAB code 
  <a href="http://www.mathworks.com/matlabcentral/fileexchange/18798">http://www.mathworks.com/matlabcentral/fileexchange/18798</a></p>
  
  <p>that does all the job automatically (and also tells if the overhead of the function is large).</p>
  
  <p>How can I call %timeit magic from a Python script (or maybe there is a better timing solution) ?</p>