pythonprofilingprofileripython

Cannot make cProfile work in IPython


I'm missing something very basic.

class C:
    def __init__(self):
        self.N = 100
        pass

    def f(self, param):
        print 'C.f -- param'
        for k in xrange(param):
            for i in xrange(self.N):
                for j in xrange(self.N):
                    a = float(i)/(1+float(j)) + float(i/self.N) ** float(j/self.N)

import cProfile

c = C()
cProfile.run('c.f(3)')

When I run the above code in IPython, I get:

NameError: name 'c' is not defined

What am I missing?

UPDATE the exact paste of my session is here: http://pastebin.com/f3e1b9946

UPDATE I didn't mention that the problem occurs in IPython, which (at it turns out) is the source of the problem


Solution

  • While inside IPython, you can use the %prun magic function:

    In [9]: %prun c.f(3)
    C.f -- param
             3 function calls in 0.066 CPU seconds
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.066    0.066    0.066    0.066 <string>:6(f)
            1    0.000    0.000    0.066    0.066 <string>:1(<module>)
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}