profiling numpy with cProfile not giving useful results

Viewed 1172

This code:

import numpy as np
import cProfile

shp = (1000,1000)
a = np.ones(shp)
o = np.zeros(shp)

def main():
    np.divide(a,1,o)
    for i in xrange(20):
        np.multiply(a,2,o)
        np.add(a,1,o)

cProfile.run('main()')

prints only:

         3 function calls in 0.269 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.269    0.269 <string>:1(<module>)
        1    0.269    0.269    0.269    0.269 testprof.py:8(main)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
iler' objects}

Can I get cProfile to work with numpy to tell me how many calls it makes to the varous np.* calls and how much time it spends on each?

edit

It is too cumbersome to wrap each of the numpy functions individually as hpaulj suggests, so I'm trying something like this to temporarily wrap many or all of the functions of interest:

def wrapper(f, fn):
    def ff(*args, **kwargs):
        return f(*args, **kwargs)
    ff.__name__ = fn
    ff.func_name = fn
    return ff

for fn in 'divide add multiply'.split():
    f = getattr(np, fn)
    setattr(np, fn, wrapper(f, fn))

but cProfile still refers to all them as ff

1 Answers
Related