author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
Thu, 02 Aug 2007 14:04:00 +0200 | |
changeset 5055 | 56d48aed1f69 |
parent 2497 | 976b6b2a1613 |
child 5992 | 30c40ba10963 |
permissions | -rw-r--r-- |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
1 |
# this is copied from the lsprof distro because somehow |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
2 |
# it is not installed by distutils |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
3 |
# |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
4 |
# small modifications made |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
5 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
6 |
import sys |
2497
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
7 |
try: |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
8 |
from _lsprof import Profiler, profiler_entry, profiler_subentry |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
9 |
except ImportError, inst: |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
10 |
import packagescan |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
11 |
if packagescan.scan_in_progress: |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
12 |
raise packagescan.SkipPackage('_lsprof not available') |
976b6b2a1613
do not try to package lsprof if not available.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2422
diff
changeset
|
13 |
raise |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
14 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
15 |
__all__ = ['profile', 'Stats'] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
16 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
17 |
def profile(f, *args, **kwds): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
18 |
"""XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
19 |
p = Profiler() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 |
p.enable(subcalls=True) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 |
try: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 |
ret = f(*args, **kwds) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 |
finally: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 |
p.disable() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 |
return ret, Stats(p.getstats()) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
26 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 |
class Stats(object): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 |
"""XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
30 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
31 |
def __init__(self, data): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
32 |
self.data = data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
33 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
34 |
def sort(self, crit="inlinetime"): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
35 |
"""XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
36 |
if crit not in profiler_entry.__dict__: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
37 |
raise ValueError, "Can't sort by %s" % crit |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
38 |
self.data.sort(lambda b, a: cmp(getattr(a, crit), |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
39 |
getattr(b, crit))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
40 |
for e in self.data: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
41 |
if e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
42 |
e.calls.sort(lambda b, a: cmp(getattr(a, crit), |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
43 |
getattr(b, crit))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
44 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
45 |
def pprint(self, top=None, file=None, limit=None, climit=None): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
46 |
"""XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
47 |
if file is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
48 |
file = sys.stdout |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
49 |
d = self.data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 |
if top is not None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 |
d = d[:top] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
52 |
cols = "% 12s %11.4f %11.4f %s\n" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
53 |
hcols = "% 12s %12s %12s %s\n" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
54 |
cols2 = "+%12s %11.4f %11.4f + %s\n" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 |
file.write(hcols % ("CallCount", "Total(s)", |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
56 |
"Inline(s)", "module:lineno(function)")) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 |
count = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
58 |
for e in d: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
59 |
file.write(cols % (e.callcount, e.totaltime, |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
60 |
e.inlinetime, label(e.code))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
61 |
count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
62 |
if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
63 |
return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 |
ccount = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 |
if e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 |
for se in e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
67 |
file.write(cols % ("+%s" % se.callcount, |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
68 |
se.totaltime, se.inlinetime, |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
69 |
"+%s" % label(se.code))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
70 |
count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
71 |
ccount += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
72 |
if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
73 |
return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
74 |
if climit is not None and ccount == climit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
75 |
break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
76 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
77 |
def freeze(self): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 |
"""Replace all references to code objects with string |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 |
descriptions; this makes it possible to pickle the instance.""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
80 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 |
# this code is probably rather ickier than it needs to be! |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 |
for i in range(len(self.data)): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
83 |
e = self.data[i] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
84 |
if not isinstance(e.code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
85 |
self.data[i] = type(e)((label(e.code),) + e[1:]) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
86 |
if e.calls: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
87 |
for j in range(len(e.calls)): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
88 |
se = e.calls[j] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
89 |
if not isinstance(se.code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
90 |
e.calls[j] = type(se)((label(se.code),) + se[1:]) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
91 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 |
_fn2mod = {} |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 |
def label(code): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 |
if isinstance(code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 |
return code |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 |
try: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
mname = _fn2mod[code.co_filename] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 |
except KeyError: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
100 |
for k, v in sys.modules.iteritems(): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 |
if v is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
102 |
continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 |
if not hasattr(v, '__file__'): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 |
continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 |
if not isinstance(v.__file__, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 |
continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
107 |
if v.__file__.startswith(code.co_filename): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
108 |
mname = _fn2mod[code.co_filename] = k |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
109 |
break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
110 |
else: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
111 |
mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
112 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
113 |
return '%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
114 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
115 |
|
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
116 |
if __name__ == '__main__': |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
117 |
import os |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
118 |
sys.argv = sys.argv[1:] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
119 |
if not sys.argv: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
120 |
print >> sys.stderr, "usage: lsprof.py <script> <arguments...>" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
121 |
sys.exit(2) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
122 |
sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0]))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 |
stats = profile(execfile, sys.argv[0], globals(), locals()) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
124 |
stats.sort() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 |
stats.pprint() |