##// END OF EJS Templates
Merge pull request #9984 from pablogsal/master...
Thomas Kluyver -
r22950:516e7be2 merge
parent child Browse files
Show More
@@ -15,6 +15,7 b' import os'
15 15 import sys
16 16 import time
17 17 import timeit
18 import math
18 19 from pdb import Restart
19 20
20 21 # cProfile was added in Python2.5
@@ -70,7 +71,6 b' class TimeitResult(object):'
70 71 compile_time: (float) time of statement compilation (s)
71 72
72 73 """
73
74 74 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
75 75 self.loops = loops
76 76 self.repeat = repeat
@@ -79,15 +79,27 b' class TimeitResult(object):'
79 79 self.all_runs = all_runs
80 80 self.compile_time = compile_time
81 81 self._precision = precision
82 self.timings = [ dt / self.loops for dt in all_runs]
83
84 @property
85 def average(self):
86 return math.fsum(self.timings) / len(self.timings)
87
88 @property
89 def stdev(self):
90 mean = self.average
91 return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
92
93 def __str__(self):
94 return (u"%s loop%s, average of %d: %s +- %s per loop (using standard deviation)"
95 % (self.loops,"" if self.loops == 1 else "s", self.repeat,
96 _format_time(self.average, self._precision),
97 _format_time(self.stdev, self._precision)))
82 98
83 99 def _repr_pretty_(self, p , cycle):
84 if self.loops == 1: # No s at "loops" if only one loop
85 unic = u"%d loop, best of %d: %s per loop" % (self.loops, self.repeat,
86 _format_time(self.best, self._precision))
87 else:
88 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
89 _format_time(self.best, self._precision))
90 p.text(u'<TimeitResult : '+unic+u'>')
100 unic = self.__str__()
101 p.text(u'<TimeitResult : '+unic+u'>')
102
91 103
92 104
93 105 class TimeitTemplateFiller(ast.NodeTransformer):
@@ -950,20 +962,20 b' python-profiler package from non-free.""")'
950 962 ::
951 963
952 964 In [1]: %timeit pass
953 10000000 loops, best of 3: 53.3 ns per loop
965 100000000 loops, average of 7: 5.48 ns +- 0.354 ns per loop (using standard deviation)
954 966
955 967 In [2]: u = None
956 968
957 969 In [3]: %timeit u is None
958 10000000 loops, best of 3: 184 ns per loop
970 10000000 loops, average of 7: 22.7 ns +- 2.33 ns per loop (using standard deviation)
959 971
960 972 In [4]: %timeit -r 4 u == None
961 1000000 loops, best of 4: 242 ns per loop
973 10000000 loops, average of 4: 27.5 ns +- 2.91 ns per loop (using standard deviation)
962 974
963 975 In [5]: import time
964 976
965 977 In [6]: %timeit -n1 time.sleep(2)
966 1 loop, best of 3: 2 s per loop
978 1 loop, average of 7: 2 s +- 4.71 µs per loop (using standard deviation)
967 979
968 980
969 981 The times reported by %timeit will be slightly higher than those
@@ -981,7 +993,8 b' python-profiler package from non-free.""")'
981 993
982 994 timefunc = timeit.default_timer
983 995 number = int(getattr(opts, "n", 0))
984 repeat = int(getattr(opts, "r", timeit.default_repeat))
996 default_repeat = 7 if timeit.default_repeat < 7 else timeit.default_repeat
997 repeat = int(getattr(opts, "r", default_repeat))
985 998 precision = int(getattr(opts, "p", 3))
986 999 quiet = 'q' in opts
987 1000 return_result = 'o' in opts
@@ -1036,22 +1049,18 b' python-profiler package from non-free.""")'
1036 1049 # This is used to check if there is a huge difference between the
1037 1050 # best and worst timings.
1038 1051 # Issue: https://github.com/ipython/ipython/issues/6471
1039 worst_tuning = 0
1040 1052 if number == 0:
1041 1053 # determine number so that 0.2 <= total time < 2.0
1042 number = 1
1043 for _ in range(1, 10):
1054 for index in range(0, 10):
1055 number = 10 ** index
1044 1056 time_number = timer.timeit(number)
1045 worst_tuning = max(worst_tuning, time_number / number)
1046 1057 if time_number >= 0.2:
1047 1058 break
1048 number *= 10
1059
1049 1060 all_runs = timer.repeat(repeat, number)
1050 1061 best = min(all_runs) / number
1051
1052 1062 worst = max(all_runs) / number
1053 if worst_tuning:
1054 worst = max(worst, worst_tuning)
1063 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1055 1064
1056 1065 if not quiet :
1057 1066 # Check best timing is greater than zero to avoid a
@@ -1063,16 +1072,13 b' python-profiler package from non-free.""")'
1063 1072 print("The slowest run took %0.2f times longer than the "
1064 1073 "fastest. This could mean that an intermediate result "
1065 1074 "is being cached." % (worst / best))
1066 if number == 1: # No s at "loops" if only one loop
1067 print(u"%d loop, best of %d: %s per loop" % (number, repeat,
1068 _format_time(best, precision)))
1069 else:
1070 print(u"%d loops, best of %d: %s per loop" % (number, repeat,
1071 _format_time(best, precision)))
1075
1076 print( timeit_result )
1077
1072 1078 if tc > tc_min:
1073 1079 print("Compiler time: %.2f s" % tc)
1074 1080 if return_result:
1075 return TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1081 return timeit_result
1076 1082
1077 1083 @skip_doctest
1078 1084 @needs_local_scope
@@ -1326,8 +1332,7 b' def parse_breakpoint(text, current_file):'
1326 1332
1327 1333 def _format_time(timespan, precision=3):
1328 1334 """Formats the timespan in a human readable form"""
1329 import math
1330
1335
1331 1336 if timespan >= 60.0:
1332 1337 # we have more than a minute, format that in a human readable form
1333 1338 # Idea from http://snipplr.com/view/5713/
@@ -649,12 +649,12 b' class TestAstTransform(unittest.TestCase):'
649 649 called.add(x)
650 650 ip.push({'f':f})
651 651
652 with tt.AssertPrints("best of "):
652 with tt.AssertPrints("average of "):
653 653 ip.run_line_magic("timeit", "-n1 f(1)")
654 654 self.assertEqual(called, {-1})
655 655 called.clear()
656
657 with tt.AssertPrints("best of "):
656
657 with tt.AssertPrints("average of "):
658 658 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
659 659 self.assertEqual(called, {-2, -3})
660 660
@@ -721,13 +721,13 b' class TestAstTransform2(unittest.TestCase):'
721 721 def f(x):
722 722 called.add(x)
723 723 ip.push({'f':f})
724
725 with tt.AssertPrints("best of "):
724
725 with tt.AssertPrints("average of "):
726 726 ip.run_line_magic("timeit", "-n1 f(1)")
727 727 self.assertEqual(called, {(1,)})
728 728 called.clear()
729
730 with tt.AssertPrints("best of "):
729
730 with tt.AssertPrints("average of "):
731 731 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
732 732 self.assertEqual(called, {(2,), (3,)})
733 733
General Comments 0
You need to be logged in to leave comments. Login now