Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 1 | """High-perfomance logging profiler, mostly written in C.""" |
| 2 | |
| 3 | import _hotshot |
| 4 | |
| 5 | from _hotshot import ProfilerError |
| 6 | |
| 7 | |
| 8 | class Profile: |
| 9 | def __init__(self, logfn, lineevents=0, linetimings=1): |
| 10 | self.lineevents = lineevents and 1 or 0 |
| 11 | self.linetimings = (linetimings and lineevents) and 1 or 0 |
| 12 | self._prof = p = _hotshot.profiler( |
| 13 | logfn, self.lineevents, self.linetimings) |
| 14 | |
| 15 | def close(self): |
| 16 | self._prof.close() |
| 17 | |
| 18 | def start(self): |
| 19 | self._prof.start() |
| 20 | |
| 21 | def stop(self): |
| 22 | self._prof.stop() |
| 23 | |
Fred Drake | 165b2cc | 2001-10-29 20:48:09 +0000 | [diff] [blame] | 24 | def addinfo(self, key, value): |
| 25 | self._prof.addinfo(key, value) |
| 26 | |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 27 | # These methods offer the same interface as the profile.Profile class, |
| 28 | # but delegate most of the work to the C implementation underneath. |
| 29 | |
| 30 | def run(self, cmd): |
| 31 | import __main__ |
| 32 | dict = __main__.__dict__ |
| 33 | return self.runctx(cmd, dict, dict) |
| 34 | |
| 35 | def runctx(self, cmd, globals, locals): |
| 36 | code = compile(cmd, "<string>", "exec") |
| 37 | self._prof.runcode(code, globals, locals) |
| 38 | return self |
| 39 | |
| 40 | def runcall(self, func, *args, **kw): |
Fred Drake | e7d8a78 | 2001-10-15 22:14:29 +0000 | [diff] [blame] | 41 | return self._prof.runcall(func, args, kw) |