##// END OF EJS Templates
Merge pull request #4153 from Carreau/timeitobj...
Thomas Kluyver -
r12758:6ee197ab merge
parent child Browse files
Show More
@@ -56,6 +56,37 b' from IPython.utils.warn import warn, error'
56 # Magic implementation classes
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 @magics_class
90 @magics_class
60 class ExecutionMagics(Magics):
91 class ExecutionMagics(Magics):
61 """Magics related to code execution, debugging, profiling, etc.
92 """Magics related to code execution, debugging, profiling, etc.
@@ -803,9 +834,9 b' python-profiler package from non-free.""")'
803 """Time execution of a Python statement or expression
834 """Time execution of a Python statement or expression
804
835
805 Usage, in line mode:
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 or in cell mode:
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 code
840 code
810 code...
841 code...
811
842
@@ -836,6 +867,11 b' python-profiler package from non-free.""")'
836 -p<P>: use a precision of <P> digits to display the timing result.
867 -p<P>: use a precision of <P> digits to display the timing result.
837 Default: 3
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 Examples
876 Examples
841 --------
877 --------
@@ -868,7 +904,7 b' python-profiler package from non-free.""")'
868
904
869 import timeit
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 posix=False, strict=False)
908 posix=False, strict=False)
873 if stmt == "" and cell is None:
909 if stmt == "" and cell is None:
874 return
910 return
@@ -877,6 +913,8 b' python-profiler package from non-free.""")'
877 number = int(getattr(opts, "n", 0))
913 number = int(getattr(opts, "n", 0))
878 repeat = int(getattr(opts, "r", timeit.default_repeat))
914 repeat = int(getattr(opts, "r", timeit.default_repeat))
879 precision = int(getattr(opts, "p", 3))
915 precision = int(getattr(opts, "p", 3))
916 quiet = 'q' in opts
917 return_result = 'o' in opts
880 if hasattr(opts, "t"):
918 if hasattr(opts, "t"):
881 timefunc = time.time
919 timefunc = time.time
882 if hasattr(opts, "c"):
920 if hasattr(opts, "c"):
@@ -948,13 +986,15 b' python-profiler package from non-free.""")'
948 if timer.timeit(number) >= 0.2:
986 if timer.timeit(number) >= 0.2:
949 break
987 break
950 number *= 10
988 number *= 10
951
989 all_runs = timer.repeat(repeat, number)
952 best = min(timer.repeat(repeat, number)) / number
990 best = min(all_runs) / number
953
991 if not quiet :
954 print u"%d loops, best of %d: %s per loop" % (number, repeat,
992 print u"%d loops, best of %d: %s per loop" % (number, repeat,
955 _format_time(best, precision))
993 _format_time(best, precision))
956 if tc > tc_min:
994 if tc > tc_min:
957 print "Compiler time: %.2f s" % tc
995 print "Compiler time: %.2f s" % tc
996 if return_result:
997 return TimeitResult(number, repeat, best, all_runs, tc, precision)
958
998
959 @skip_doctest
999 @skip_doctest
960 @needs_local_scope
1000 @needs_local_scope
@@ -487,7 +487,21 b' def test_timeit_special_syntax():'
487 # cell mode test
487 # cell mode test
488 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
488 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
489 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
489 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
490
490
491 def test_timeit_return():
492 """
493 test wether timeit -o return object
494 """
495
496 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
497 assert(res is not None)
498
499 def test_timeit_quiet():
500 """
501 test quiet option of timeit magic
502 """
503 with tt.AssertNotPrints("loops"):
504 _ip.run_cell("%timeit -n1 -r1 -q 1")
491
505
492 @dec.skipif(execution.profile is None)
506 @dec.skipif(execution.profile is None)
493 def test_prun_special_syntax():
507 def test_prun_special_syntax():
General Comments 0
You need to be logged in to leave comments. Login now