Skip to content

Instantly share code, notes, and snippets.

@tsungtwu
Last active August 20, 2020 09:36
Show Gist options
  • Save tsungtwu/6bf5cd04f770b6da08cba6943abaaf31 to your computer and use it in GitHub Desktop.
Save tsungtwu/6bf5cd04f770b6da08cba6943abaaf31 to your computer and use it in GitHub Desktop.
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print('{:s} function took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0))
return ret
return wrap
def line_profiler(view=None, extra_view=None):
import line_profiler
def wrapper(view):
def wrapped(*args, **kwargs):
prof = line_profiler.LineProfiler()
prof.add_function(view)
if extra_view:
[prof.add_function(v) for v in extra_view]
with prof:
resp = view(*args, **kwargs)
prof.print_stats()
return resp
return wrapped
if view:
return wrapper(view)
return wrapper
def profileit(func):
import cProfile
import functools
@functools.wraps(func) # <-- Changes here.
def wrapper(*args, **kwargs):
datafn = func.__name__ + ".profile" # Name the data file sensibly
prof = cProfile.Profile()
retval = prof.runcall(func, *args, **kwargs)
prof.dump_stats(datafn)
return retval
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment