Skip to content

Instantly share code, notes, and snippets.

@methane
Created April 17, 2012 12:02
Show Gist options
  • Select an option

  • Save methane/2405573 to your computer and use it in GitHub Desktop.

Select an option

Save methane/2405573 to your computer and use it in GitHub Desktop.
プロファイラを使って関数のプロファイルをとるサンプル
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