|
| 1 | +""" |
| 2 | + A very simple profiling class. Define some timers and methods |
| 3 | + to start and stop them. Nesting of timers is tracked so we can |
| 4 | + pretty print the profiling information. |
| 5 | +
|
| 6 | + # define a timer object, labeled 'my timer' |
| 7 | + a = timer('my timer') |
| 8 | +
|
| 9 | + This will add 'my timer' to the list of keys in the 'my timer' |
| 10 | + dictionary. Subsequent calls to the timer class constructor |
| 11 | + will have no effect. |
| 12 | +
|
| 13 | + # start timing the 'my timer' block of code |
| 14 | + a.begin() |
| 15 | +
|
| 16 | + ... do stuff here ... |
| 17 | +
|
| 18 | + # end the timing of the 'my timer' block of code |
| 19 | + a.end() |
| 20 | +
|
| 21 | + for best results, the block of code timed should be large |
| 22 | + enough to offset the overhead of the timer class method |
| 23 | + calls. |
| 24 | +
|
| 25 | + Multiple timers can be instanciated and nested. The stackCount |
| 26 | + global parameter keeps count of the level of nesting, and the |
| 27 | + timerNesting data structure stores the nesting level for each |
| 28 | + defined timer. |
| 29 | +
|
| 30 | + timeReport() is called at the end to print out a summary of the |
| 31 | + timing. |
| 32 | +
|
| 33 | + At present, no enforcement is done to ensure proper nesting. |
| 34 | +
|
| 35 | +""" |
| 36 | + |
| 37 | +from __future__ import print_function |
| 38 | + |
| 39 | +import time |
| 40 | + |
| 41 | +timers = {} |
| 42 | + |
| 43 | +# keep basic count of how nested we are in the timers, so we can do some |
| 44 | +# pretty printing. |
| 45 | +stack_count = 0 |
| 46 | + |
| 47 | +timer_nesting = {} |
| 48 | +timer_order = [] |
| 49 | + |
| 50 | +class Timer(object): |
| 51 | + |
| 52 | + def __init__ (self, name): |
| 53 | + global timers, stack_count, timer_nesting, timer_order |
| 54 | + |
| 55 | + self.name = name |
| 56 | + |
| 57 | + keys = timers.keys() |
| 58 | + |
| 59 | + if name not in keys: |
| 60 | + timers[name] = 0.0 |
| 61 | + self.startTime = 0.0 |
| 62 | + timer_order.append(name) |
| 63 | + timer_nesting[name] = stack_count |
| 64 | + |
| 65 | + |
| 66 | + def begin(self): |
| 67 | + global stack_count |
| 68 | + |
| 69 | + self.startTime = time.time() |
| 70 | + stack_count += 1 |
| 71 | + |
| 72 | + |
| 73 | + def end(self): |
| 74 | + global timers, stack_count |
| 75 | + |
| 76 | + elapsedTime = time.time() - self.startTime |
| 77 | + timers[self.name] += elapsedTime |
| 78 | + |
| 79 | + stack_count -= 1 |
| 80 | + |
| 81 | + |
| 82 | +def time_report(): |
| 83 | + global timers, timer_order, timer_nesting |
| 84 | + |
| 85 | + spacing = ' ' |
| 86 | + for key in timer_order: |
| 87 | + print(timer_nesting[key]*spacing + key + ': ', timers[key]) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + a = Timer('1') |
| 93 | + a.begin() |
| 94 | + time.sleep(10.) |
| 95 | + a.end() |
| 96 | + |
| 97 | + b = Timer('2') |
| 98 | + b.begin() |
| 99 | + time.sleep(5.) |
| 100 | + |
| 101 | + c = Timer('3') |
| 102 | + c.begin() |
| 103 | + |
| 104 | + time.sleep(20.) |
| 105 | + |
| 106 | + b.end() |
| 107 | + c.end() |
| 108 | + |
| 109 | + time_report() |
0 commit comments