Created
January 2, 2015 15:05
-
-
Save lebedov/7a9df4510d7a4473eff1 to your computer and use it in GitHub Desktop.
Decorator for profiling functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import cProfile as profile | |
import wrapt | |
def prof(*dec_args, **dec_kwargs): | |
pr = profile.Profile() | |
if not dec_args or not inspect.isfunction(dec_args[0]): | |
@wrapt.decorator | |
def wrapper(wrapped, instance, args, kwargs): | |
pr.enable() | |
result = wrapped(*args, **kwargs) | |
pr.disable() | |
if dec_kwargs.has_key('filename'): | |
pr.dump_stats(dec_kwargs['filename']) | |
else: | |
pr.print_stats() | |
return result | |
return wrapper | |
else: | |
@wrapt.decorator | |
def wrapper(wrapped, instance, args, kwargs): | |
pr.enable() | |
result = wrapped(*args, **kwargs) | |
pr.disable() | |
pr.print_stats() | |
return result | |
return wrapper(dec_args[0]) | |
if __name__ == '__main__': | |
import numpy as np | |
@prof | |
def foo(N): | |
return np.linalg.pinv(np.random.rand(N, N)) | |
@prof(filename='out.prof') | |
def bar(N): | |
return np.linalg.pinv(np.random.rand(N, N)) | |
foo(100) | |
bar(100) | |
# Also works with methods: | |
class Foo(object): | |
@prof | |
def foo(self, N): | |
return np.linalg.pinv(np.random.rand(N, N)) | |
f = Foo() | |
f.foo(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment