profiling numpy with cProfile not giving useful results

匆匆过客 提交于 2019-12-10 16:20:00

问题


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:


How about wrapping the relevant calls in Python functions?

def mul(*args):
    np.multiply(*args)
def add(*args):
    np.add(*args)

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

That's basically the idea in this SO thread about improving profiling granularity - it profiles function calls, not lines.

Does effective Cython cProfiling imply writing many sub functions?



来源:https://stackoverflow.com/questions/20434042/profiling-numpy-with-cprofile-not-giving-useful-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!