How to run an IPython magic from a script (or timing a Python script) -


the ipython %timeit magic command job measuring time required run python code. now, want use analogous in python script. know timeit module, however, has several disadvantages, example, how select number of runs adaptively? i.e., default code

import timeit t=timeit.timer("code(f)", "from __main__ import code,f") t.timeit()  

runs code million times. %timeit ipyhton magic command automatically. suggest use matlab code http://www.mathworks.com/matlabcentral/fileexchange/18798

that job automatically (and tells if overhead of function large).

how can call %timeit magic python script (or maybe there better timing solution) ?

it depends bit on version of ipython have. if have 1.x:

from ipython import get_ipython ipython = get_ipython() 

if have older version:

import ipython.core.ipapi   ipython = ipython.core.ipapi.get() 

or

import ipython.ipapi   ipython = ipython.ipapi.get() 

once that's done, run magic command this:

ipython.magic("timeit abs(-42)") 

note script must run via ipython.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -