[IPython-dev] Feature request/how?

Fernando.Perez at colorado.edu Fernando.Perez at colorado.edu
Tue Feb 22 01:44:24 EST 2005

Quoting Charles Moad <cmoad at indiana.edu>:

> 	Thanks for the pointer.  I am still fuzzy on how I would create a magic
> function using this, since I need to run the parameter_s arg of the
> magic function in the current context.  timing/timings takes the
> actually function and args, not a string.

It was quicker to just write the code rather than to explain how to do it.  Here
it is, it will go into the .12 release.  I still need to write a docstring for
it (that always takes longer than the actual code).



    def magic_time(self,parameter_s = ''):
        """Time execution of an expression which can be passed to eval().

        The value of the expression is returned."""

        # fail immediately if the given expression can't be compiled
        code = compile(parameter_s,'<timed evaluation>','eval')
        # skew measurement as little as possible
        glob = self.shell.user_ns
        clk = clock
        # time execution
        s = clk()
        out = eval(code,glob)
        tot = clk()-s
        print "Call time: %.2f s." % tot
        return out

More information about the IPython-dev mailing list