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