pythonprofilers / memory_profiler

Monitor Memory usage of Python code
http://pypi.python.org/pypi/memory_profiler
Other
4.39k stars 380 forks source link

Use like line_profiler (and Cython-support) #291

Open bergkvist opened 4 years ago

bergkvist commented 4 years ago

Trying to use it like line_profiler doesn't work

from memory_profiler import LineProfiler
profile = LineProfiler(_geometry_from_xy)
profile.runcall(_geometry_from_xy, lon, lat, coord_count)
profile.print_stats()

I get an error

Traceback (most recent call last):
  File "kognitwin/geometry/profile.py", line 20, in <module>
    profile = LineProfiler(_geometry_from_xy)
TypeError: __init__() takes 1 positional argument but 2 were given

Note that I cannot use the @profile decorator - because I'm trying to profile a Cython function. I also want the profiling to run as part of my CI/CD - without being present in production code.

sksavant commented 4 years ago

The following snippet might work. Not sure about Cython support though

from memory_profiler import LineProfiler, show_results
profile = LineProfiler()
profile.add_function(_geometry_from_xy)
profile(_geometry_from_xy)(lon, lat, coord_count)
show_results(profile)