##// END OF EJS Templates
Refactor TimeitResult
Pablo Galindo -
Show More
@@ -72,27 +72,45 b' class TimeitResult(object):'
72
72
73 """
73 """
74
74
75 def __init__(self, loops, repeat, average, stdev, all_runs, compile_time, precision):
75 def __init__(self, loops, repeat, all_runs, compile_time, precision):
76 self.loops = loops
76 self.loops = loops
77 self.repeat = repeat
77 self.repeat = repeat
78 self.average = average
79 self.stdev = stdev
80 self.all_runs = all_runs
78 self.all_runs = all_runs
81 self.compile_time = compile_time
79 self.compile_time = compile_time
82 self._precision = precision
80 self._precision = precision
83
81 self.timings = [ dt / self.loops for dt in all_runs]
84 def _repr_pretty_(self, p , cycle):
82 self._average = None
83 self._stdev = None
84
85 @property
86 def average(self):
87 if self._average is None:
88 self._average = math.fsum(self.timings) / len(self.timings)
89 return self._average
90
91 @property
92 def stdev(self):
93 if self._stdev is None:
94 mean = self.average
95 self._stdev = (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
96 return self._stdev
97
98 def __str__(self):
85 if self.loops == 1: # No s at "loops" if only one loop
99 if self.loops == 1: # No s at "loops" if only one loop
86 unic = (u"%s loop, average of %d: %s +- %s per loop (using standard deviation)"
100 return (u"%s loop, average of %d: %s +- %s per loop (using standard deviation)"
87 % (self.loops, self.repeat,
101 % (self.loops, self.repeat,
88 _format_time(self.average, self._precision),
102 _format_time(self.average, self._precision),
89 _format_time(self.stdev, self._precision)))
103 _format_time(self.stdev, self._precision)))
90 else:
104 else:
91 unic = (u"%s loops, average of %d: %s +- %s per loop (using standard deviation)"
105 return (u"%s loops, average of %d: %s +- %s per loop (using standard deviation)"
92 % (self.loops, self.repeat,
106 % (self.loops, self.repeat,
93 _format_time(self.average, self._precision),
107 _format_time(self.average, self._precision),
94 _format_time(self.stdev, self._precision)))
108 _format_time(self.stdev, self._precision)))
95 p.text(u'<TimeitResult : '+unic+u'>')
109
110 def _repr_pretty_(self, p , cycle):
111 unic = self.__str__()
112 p.text(u'<TimeitResult : '+unic+u'>')
113
96
114
97
115
98 class TimeitTemplateFiller(ast.NodeTransformer):
116 class TimeitTemplateFiller(ast.NodeTransformer):
@@ -1051,17 +1069,7 b' python-profiler package from non-free.""")'
1051 break
1069 break
1052
1070
1053 all_runs = timer.repeat(repeat, number)
1071 all_runs = timer.repeat(repeat, number)
1054 timings = [ dt / number for dt in all_runs]
1072 timeit_result = TimeitResult(number, repeat, all_runs, tc, precision)
1055
1056 def _avg(numbers):
1057 return math.fsum(numbers) / len(numbers)
1058
1059 def _stdev(numbers):
1060 mean = _avg(numbers)
1061 return (math.fsum([(x - mean) ** 2 for x in numbers]) / len(numbers)) ** 0.5
1062
1063 average = _avg(timings)
1064 stdev = _stdev(timings)
1065
1073
1066 if not quiet :
1074 if not quiet :
1067 # Check best timing is greater than zero to avoid a
1075 # Check best timing is greater than zero to avoid a
@@ -1069,20 +1077,12 b' python-profiler package from non-free.""")'
1069 # In cases where the slowest timing is lesser than a micosecond
1077 # In cases where the slowest timing is lesser than a micosecond
1070 # we assume that it does not really matter if the fastest
1078 # we assume that it does not really matter if the fastest
1071 # timing is 4 times faster than the slowest timing or not.
1079 # timing is 4 times faster than the slowest timing or not.
1072 if number == 1: # No s at "loops" if only one loop
1080 print( timeit_result )
1073 print(u"%s loop, average of %d: %s +- %s per loop (using standard deviation)"
1081
1074 % (number, repeat,
1082 if tc > tc_min:
1075 _format_time(average, precision),
1076 _format_time(stdev, precision)))
1077 else:
1078 print(u"%s loops, average of %d: %s +- %s per loop (using standard deviation)"
1079 % (number, repeat,
1080 _format_time(average, precision),
1081 _format_time(stdev, precision)))
1082 if tc > tc_min:
1083 print("Compiler time: %.2f s" % tc)
1083 print("Compiler time: %.2f s" % tc)
1084 if return_result:
1084 if return_result:
1085 return TimeitResult(number, repeat, average, stdev, all_runs, tc, precision)
1085 return timeit_result
1086
1086
1087 @skip_doctest
1087 @skip_doctest
1088 @needs_local_scope
1088 @needs_local_scope
General Comments 0
You need to be logged in to leave comments. Login now