##// END OF EJS Templates
time magic: make it usable as cell magic...
Jan Schulz -
Show More
@@ -836,35 +836,42 b' python-profiler package from non-free.""")'
836
836
837 @skip_doctest
837 @skip_doctest
838 @needs_local_scope
838 @needs_local_scope
839 @line_magic
839 @line_cell_magic
840 def time(self,parameter_s, local_ns=None):
840 def time(self,line='', cell=None, local_ns=None):
841 """Time execution of a Python statement or expression.
841 """Time execution of a Python statement or expression.
842
842
843 The CPU and wall clock times are printed, and the value of the
843 The CPU and wall clock times are printed, and the value of the
844 expression (if any) is returned. Note that under Win32, system time
844 expression (if any) is returned. Note that under Win32, system time
845 is always reported as 0, since it can not be measured.
845 is always reported as 0, since it can not be measured.
846
847 This function can be used both as a line and cell magic:
848
849 - In line mode you can time a single-line statement (though multiple
850 ones can be chained with using semicolons).
851
852 - In cell mode, you can time the cell body (a directly
853 following statement raises an error).
846
854
847 This function provides very basic timing functionality. In Python
855 This function provides very basic timing functionality. Use the timeit
848 2.3, the timeit module offers more control and sophistication, so this
856 magic for more controll over the measurement.
849 could be rewritten to use it (patches welcome).
850
857
851 Examples
858 Examples
852 --------
859 --------
853 ::
860 ::
854
861
855 In [1]: time 2**128
862 In [1]: %time 2**128
856 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
863 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
857 Wall time: 0.00
864 Wall time: 0.00
858 Out[1]: 340282366920938463463374607431768211456L
865 Out[1]: 340282366920938463463374607431768211456L
859
866
860 In [2]: n = 1000000
867 In [2]: n = 1000000
861
868
862 In [3]: time sum(range(n))
869 In [3]: %time sum(range(n))
863 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
870 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
864 Wall time: 1.37
871 Wall time: 1.37
865 Out[3]: 499999500000L
872 Out[3]: 499999500000L
866
873
867 In [4]: time print 'hello world'
874 In [4]: %time print 'hello world'
868 hello world
875 hello world
869 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
876 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
870 Wall time: 0.00
877 Wall time: 0.00
@@ -875,19 +882,25 b' python-profiler package from non-free.""")'
875 the expression can take a noticeable amount of time to compute, that
882 the expression can take a noticeable amount of time to compute, that
876 time is purely due to the compilation:
883 time is purely due to the compilation:
877
884
878 In [5]: time 3**9999;
885 In [5]: %time 3**9999;
879 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
886 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
880 Wall time: 0.00 s
887 Wall time: 0.00 s
881
888
882 In [6]: time 3**999999;
889 In [6]: %time 3**999999;
883 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
890 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
884 Wall time: 0.00 s
891 Wall time: 0.00 s
885 Compiler : 0.78 s
892 Compiler : 0.78 s
886 """
893 """
887
894
888 # fail immediately if the given expression can't be compiled
895 # fail immediately if the given expression can't be compiled
889
896
890 expr = self.shell.prefilter(parameter_s,False)
897 if line and cell:
898 raise UsageError("Can't use statement directly after '%%time'!")
899
900 if cell:
901 expr = self.shell.prefilter(cell,False)
902 else:
903 expr = self.shell.prefilter(line,False)
891
904
892 # Minimum time above which parse time will be reported
905 # Minimum time above which parse time will be reported
893 tp_min = 0.1
906 tp_min = 0.1
General Comments 0
You need to be logged in to leave comments. Login now