Show More
@@ -1,28 +1,19 b'' | |||||
1 | # this is copied from the lsprof distro because somehow |
|
1 | #! /usr/bin/env python | |
2 | # it is not installed by distutils |
|
|||
3 | # |
|
|||
4 | # small modifications made |
|
|||
5 |
|
2 | |||
6 | import sys |
|
3 | import sys | |
7 | try: |
|
4 | from _lsprof import Profiler, profiler_entry, profiler_subentry | |
8 | from _lsprof import Profiler, profiler_entry, profiler_subentry |
|
|||
9 | except ImportError, inst: |
|
|||
10 | import packagescan |
|
|||
11 | if packagescan.scan_in_progress: |
|
|||
12 | raise packagescan.SkipPackage('_lsprof not available') |
|
|||
13 | raise |
|
|||
14 |
|
5 | |||
15 | __all__ = ['profile', 'Stats'] |
|
6 | __all__ = ['profile', 'Stats'] | |
16 |
|
7 | |||
17 | def profile(f, *args, **kwds): |
|
8 | def profile(f, *args, **kwds): | |
18 | """XXX docstring""" |
|
9 | """XXX docstring""" | |
19 | p = Profiler() |
|
10 | p = Profiler() | |
20 | p.enable(subcalls=True) |
|
11 | p.enable(subcalls=True, builtins=True) | |
21 | try: |
|
12 | try: | |
22 |
|
|
13 | f(*args, **kwds) | |
23 | finally: |
|
14 | finally: | |
24 | p.disable() |
|
15 | p.disable() | |
25 |
return |
|
16 | return Stats(p.getstats()) | |
26 |
|
17 | |||
27 |
|
18 | |||
28 | class Stats(object): |
|
19 | class Stats(object): | |
@@ -49,14 +40,14 b' class Stats(object):' | |||||
49 | d = self.data |
|
40 | d = self.data | |
50 | if top is not None: |
|
41 | if top is not None: | |
51 | d = d[:top] |
|
42 | d = d[:top] | |
52 | cols = "% 12s %11.4f %11.4f %s\n" |
|
43 | cols = "% 12s %12s %11.4f %11.4f %s\n" | |
53 | hcols = "% 12s %12s %12s %s\n" |
|
44 | hcols = "% 12s %12s %12s %12s %s\n" | |
54 | cols2 = "+%12s %11.4f %11.4f + %s\n" |
|
45 | cols2 = "+%12s %12s %11.4f %11.4f + %s\n" | |
55 | file.write(hcols % ("CallCount", "Total(s)", |
|
46 | file.write(hcols % ("CallCount", "Recursive", "Total(ms)", | |
56 | "Inline(s)", "module:lineno(function)")) |
|
47 | "Inline(ms)", "module:lineno(function)")) | |
57 | count = 0 |
|
48 | count = 0 | |
58 | for e in d: |
|
49 | for e in d: | |
59 | file.write(cols % (e.callcount, e.totaltime, |
|
50 | file.write(cols % (e.callcount, e.reccallcount, e.totaltime, | |
60 | e.inlinetime, label(e.code))) |
|
51 | e.inlinetime, label(e.code))) | |
61 | count += 1 |
|
52 | count += 1 | |
62 | if limit is not None and count == limit: |
|
53 | if limit is not None and count == limit: | |
@@ -64,7 +55,7 b' class Stats(object):' | |||||
64 | ccount = 0 |
|
55 | ccount = 0 | |
65 | if e.calls: |
|
56 | if e.calls: | |
66 | for se in e.calls: |
|
57 | for se in e.calls: | |
67 | file.write(cols % ("+%s" % se.callcount, |
|
58 | file.write(cols % ("+%s" % se.callcount, se.reccallcount, | |
68 | se.totaltime, se.inlinetime, |
|
59 | se.totaltime, se.inlinetime, | |
69 | "+%s" % label(se.code))) |
|
60 | "+%s" % label(se.code))) | |
70 | count += 1 |
|
61 | count += 1 | |
@@ -83,11 +74,11 b' class Stats(object):' | |||||
83 | e = self.data[i] |
|
74 | e = self.data[i] | |
84 | if not isinstance(e.code, str): |
|
75 | if not isinstance(e.code, str): | |
85 | self.data[i] = type(e)((label(e.code),) + e[1:]) |
|
76 | self.data[i] = type(e)((label(e.code),) + e[1:]) | |
86 |
|
|
77 | if e.calls: | |
87 |
|
|
78 | for j in range(len(e.calls)): | |
88 |
|
|
79 | se = e.calls[j] | |
89 |
|
|
80 | if not isinstance(se.code, str): | |
90 |
|
|
81 | e.calls[j] = type(se)((label(se.code),) + se[1:]) | |
91 |
|
82 | |||
92 | _fn2mod = {} |
|
83 | _fn2mod = {} | |
93 |
|
84 | |||
@@ -97,7 +88,7 b' def label(code):' | |||||
97 | try: |
|
88 | try: | |
98 | mname = _fn2mod[code.co_filename] |
|
89 | mname = _fn2mod[code.co_filename] | |
99 | except KeyError: |
|
90 | except KeyError: | |
100 |
for k, v in sys.modules. |
|
91 | for k, v in sys.modules.items(): | |
101 | if v is None: |
|
92 | if v is None: | |
102 | continue |
|
93 | continue | |
103 | if not hasattr(v, '__file__'): |
|
94 | if not hasattr(v, '__file__'): |
General Comments 0
You need to be logged in to leave comments.
Login now