##// END OF EJS Templates
make timeit optionally return an object...
Matthias BUSSONNIER -
Show More
@@ -56,6 +56,37 b' from IPython.utils.warn import warn, error'
56 56 # Magic implementation classes
57 57 #-----------------------------------------------------------------------------
58 58
59
60 class TimeitResult(object):
61 """
62 Object returned by the timeit magic with info about the run.
63
64 Contain the following attributes :
65
66 loops: (int) number of loop done per measurement
67 repeat: (int) number of time the mesurement has been repeated
68 best: (float) best execusion time / number
69 all_runs: (list of float) execusion time of each run (in s)
70 compile_time: (float) time of statement compilation (s)
71
72 """
73
74 def __init__(self, loops, repeat, best, all_runs, compile_time, precision):
75 self.loops = loops
76 self.repeat = repeat
77 self.best = best
78 self.all_runs = all_runs
79 self.compile_time = compile_time
80 self._precision = precision
81
82 def _repr_pretty_(self, p , cycle):
83 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
84 _format_time(self.best, self._precision))
85 p.text(u'<TimeitResult : '+unic+u'>')
86
87
88
89
59 90 @magics_class
60 91 class ExecutionMagics(Magics):
61 92 """Magics related to code execution, debugging, profiling, etc.
@@ -803,9 +834,9 b' python-profiler package from non-free.""")'
803 834 """Time execution of a Python statement or expression
804 835
805 836 Usage, in line mode:
806 %timeit [-n<N> -r<R> [-t|-c]] statement
837 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
807 838 or in cell mode:
808 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
839 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
809 840 code
810 841 code...
811 842
@@ -836,6 +867,11 b' python-profiler package from non-free.""")'
836 867 -p<P>: use a precision of <P> digits to display the timing result.
837 868 Default: 3
838 869
870 -q: Quiet, do not print result.
871
872 -o: return a TimeitResult that can be stored in a variable to inspect
873 the result in more details.
874
839 875
840 876 Examples
841 877 --------
@@ -868,7 +904,7 b' python-profiler package from non-free.""")'
868 904
869 905 import timeit
870 906
871 opts, stmt = self.parse_options(line,'n:r:tcp:',
907 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
872 908 posix=False, strict=False)
873 909 if stmt == "" and cell is None:
874 910 return
@@ -877,6 +913,8 b' python-profiler package from non-free.""")'
877 913 number = int(getattr(opts, "n", 0))
878 914 repeat = int(getattr(opts, "r", timeit.default_repeat))
879 915 precision = int(getattr(opts, "p", 3))
916 quiet = 'q' in opts
917 return_result = 'o' in opts
880 918 if hasattr(opts, "t"):
881 919 timefunc = time.time
882 920 if hasattr(opts, "c"):
@@ -948,13 +986,15 b' python-profiler package from non-free.""")'
948 986 if timer.timeit(number) >= 0.2:
949 987 break
950 988 number *= 10
951
952 best = min(timer.repeat(repeat, number)) / number
953
954 print u"%d loops, best of %d: %s per loop" % (number, repeat,
955 _format_time(best, precision))
956 if tc > tc_min:
957 print "Compiler time: %.2f s" % tc
989 all_runs = timer.repeat(repeat, number)
990 best = min(all_runs) / number
991 if not quiet :
992 print u"%d loops, best of %d: %s per loop" % (number, repeat,
993 _format_time(best, precision))
994 if tc > tc_min:
995 print "Compiler time: %.2f s" % tc
996 if return_result:
997 return TimeitResult(number, repeat, best, all_runs, tc, precision)
958 998
959 999 @skip_doctest
960 1000 @needs_local_scope
General Comments 0
You need to be logged in to leave comments. Login now