Object returned by the timeit magic with info about the run. Contains the following attributes : loops: (int) number of loops done per measurement repeat: (int) number of times the measurement has been repeated best: (float) best execution time / number all_runs: (list of
| 64 | |
| 65 | |
| 66 | class TimeitResult(object): |
| 67 | """ |
| 68 | Object returned by the timeit magic with info about the run. |
| 69 | |
| 70 | Contains the following attributes : |
| 71 | |
| 72 | loops: (int) number of loops done per measurement |
| 73 | repeat: (int) number of times the measurement has been repeated |
| 74 | best: (float) best execution time / number |
| 75 | all_runs: (list of float) execution time of each run (in s) |
| 76 | compile_time: (float) time of statement compilation (s) |
| 77 | |
| 78 | """ |
| 79 | def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision): |
| 80 | self.loops = loops |
| 81 | self.repeat = repeat |
| 82 | self.best = best |
| 83 | self.worst = worst |
| 84 | self.all_runs = all_runs |
| 85 | self.compile_time = compile_time |
| 86 | self._precision = precision |
| 87 | self.timings = [ dt / self.loops for dt in all_runs] |
| 88 | |
| 89 | @property |
| 90 | def average(self): |
| 91 | return math.fsum(self.timings) / len(self.timings) |
| 92 | |
| 93 | @property |
| 94 | def stdev(self): |
| 95 | mean = self.average |
| 96 | return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5 |
| 97 | |
| 98 | def __str__(self): |
| 99 | pm = '+-' |
| 100 | if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: |
| 101 | try: |
| 102 | u'\xb1'.encode(sys.stdout.encoding) |
| 103 | pm = u'\xb1' |
| 104 | except: |
| 105 | pass |
| 106 | return ( |
| 107 | u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)" |
| 108 | .format( |
| 109 | pm = pm, |
| 110 | runs = self.repeat, |
| 111 | loops = self.loops, |
| 112 | loop_plural = "" if self.loops == 1 else "s", |
| 113 | run_plural = "" if self.repeat == 1 else "s", |
| 114 | mean = _format_time(self.average, self._precision), |
| 115 | std = _format_time(self.stdev, self._precision)) |
| 116 | ) |
| 117 | |
| 118 | def _repr_pretty_(self, p , cycle): |
| 119 | unic = self.__str__() |
| 120 | p.text(u'<TimeitResult : '+unic+u'>') |
| 121 | |
| 122 | |
| 123 | class TimeitTemplateFiller(ast.NodeTransformer): |