Created
April 17, 2012 12:02
-
-
Save methane/2405573 to your computer and use it in GitHub Desktop.
プロファイラを使って関数のプロファイルをとるサンプル
This file contains hidden or 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
| from cProfile import Profile | |
| from functools import wraps | |
| import atexit | |
| _prof = Profile(builtins=False) | |
| def with_profile(func): | |
| @wraps(func) | |
| def wrapped(*args, **kw): | |
| return _prof.runcall(func, *args, **kw) | |
| return wrapped | |
| @with_profile | |
| def mysum(X): | |
| t = 0 | |
| for x in X: | |
| t += x | |
| return t | |
| r=range(1000000) | |
| mysum(r) | |
| mysum(r) | |
| mysum(r) | |
| def get_profile(): | |
| import marshal | |
| _prof.create_stats() | |
| return marshal.dump(_prof.stats, open('/tmp/profile.out', 'wb')) | |
| atexit.register(get_profile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment