Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from decorator import decorator
from line_profiler import LineProfiler
@decorator
def profile_each_line(func, *args, **kwargs):
profiler = LineProfiler()
profiled_func = profiler(func)
try:
profiled_func(*args, **kwargs)
finally:
profiler.print_stats()
@un1t

This comment has been minimized.

Copy link

@un1t un1t commented Nov 19, 2014

Thank for snippet. Here is little improvement

return profiled_func(*args, **kwargs)
@sibelius

This comment has been minimized.

Copy link

@sibelius sibelius commented Oct 21, 2015

it's not working

@saurabh-kataria

This comment has been minimized.

Copy link

@saurabh-kataria saurabh-kataria commented Jun 25, 2020

I am getting this error: AttributeError: 'NoneType' object has no attribute '__code__'

def a():
    x=np.ones(100000)
    b()
def b():
    y=np.ones(20000)
profile_each_line(a())
@jthistle

This comment has been minimized.

Copy link

@jthistle jthistle commented Aug 16, 2020

@saurabh-kataria: you need to write:

@profile_each_line
def a():
    x=np.ones(100000)
    b()

def b():
    y=np.ones(20000)

a() in your snippet returns None (which is what all Python functions return by default), which is why you're getting the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment