##// END OF EJS Templates
Track and report compilation time if significant....
fperez -
Show More
@@ -1,7 +1,7 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 2763 2007-09-14 06:35:44Z fperez $"""
4 $Id: Magic.py 2841 2007-10-10 00:17:26Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1752,7 +1752,14 b' Currently the magic system has the following functions:\\n"""'
1752
1752
1753 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1753 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1754 'setup': "pass"}
1754 'setup': "pass"}
1755 # Track compilation time so it can be reported if too long
1756 # Minimum time above which compilation time will be reported
1757 tc_min = 0.1
1758
1759 t0 = clock()
1755 code = compile(src, "<magic-timeit>", "exec")
1760 code = compile(src, "<magic-timeit>", "exec")
1761 tc = clock()-t0
1762
1756 ns = {}
1763 ns = {}
1757 exec code in self.shell.user_ns, ns
1764 exec code in self.shell.user_ns, ns
1758 timer.inner = ns["inner"]
1765 timer.inner = ns["inner"]
@@ -1775,6 +1782,8 b' Currently the magic system has the following functions:\\n"""'
1775 precision,
1782 precision,
1776 best * scaling[order],
1783 best * scaling[order],
1777 units[order])
1784 units[order])
1785 if tc > tc_min:
1786 print "Compiler time: %.2f s" % tc
1778
1787
1779 def magic_time(self,parameter_s = ''):
1788 def magic_time(self,parameter_s = ''):
1780 """Time execution of a Python statement or expression.
1789 """Time execution of a Python statement or expression.
@@ -1805,18 +1814,40 b' Currently the magic system has the following functions:\\n"""'
1805 hello world
1814 hello world
1806 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1815 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1807 Wall time: 0.00
1816 Wall time: 0.00
1817
1818 Note that the time needed by Python to compile the given expression
1819 will be reported if it is more than 0.1s. In this example, the
1820 actual exponentiation is done by Python at compilation time, so while
1821 the expression can take a noticeable amount of time to compute, that
1822 time is purely due to the compilation:
1823
1824 In [5]: time 3**9999;
1825 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1826 Wall time: 0.00 s
1827
1828 In [6]: time 3**999999;
1829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1830 Wall time: 0.00 s
1831 Compiler : 0.78 s
1808 """
1832 """
1809
1833
1810 # fail immediately if the given expression can't be compiled
1834 # fail immediately if the given expression can't be compiled
1811
1835
1812 expr = self.shell.prefilter(parameter_s,False)
1836 expr = self.shell.prefilter(parameter_s,False)
1837
1838 # Minimum time above which compilation time will be reported
1839 tc_min = 0.1
1813
1840
1814 try:
1841 try:
1815 mode = 'eval'
1842 mode = 'eval'
1843 t0 = clock()
1816 code = compile(expr,'<timed eval>',mode)
1844 code = compile(expr,'<timed eval>',mode)
1845 tc = clock()-t0
1817 except SyntaxError:
1846 except SyntaxError:
1818 mode = 'exec'
1847 mode = 'exec'
1848 t0 = clock()
1819 code = compile(expr,'<timed exec>',mode)
1849 code = compile(expr,'<timed exec>',mode)
1850 tc = clock()-t0
1820 # skew measurement as little as possible
1851 # skew measurement as little as possible
1821 glob = self.shell.user_ns
1852 glob = self.shell.user_ns
1822 clk = clock2
1853 clk = clock2
@@ -1840,7 +1871,9 b' Currently the magic system has the following functions:\\n"""'
1840 cpu_tot = cpu_user+cpu_sys
1871 cpu_tot = cpu_user+cpu_sys
1841 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1872 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1842 (cpu_user,cpu_sys,cpu_tot)
1873 (cpu_user,cpu_sys,cpu_tot)
1843 print "Wall time: %.2f" % wall_time
1874 print "Wall time: %.2f s" % wall_time
1875 if tc > tc_min:
1876 print "Compiler : %.2f s" % tc
1844 return out
1877 return out
1845
1878
1846 def magic_macro(self,parameter_s = ''):
1879 def magic_macro(self,parameter_s = ''):
@@ -1,3 +1,9 b''
1 2007-10-09 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Magic.py (magic_time): track compilation time and report
4 it if longer than 0.1s (fix done to %time and %timeit). After a
5 SAGE bug report: http://trac.sagemath.org/sage_trac/ticket/632.
6
1 2007-09-18 Ville Vainio <vivainio@gmail.com>
7 2007-09-18 Ville Vainio <vivainio@gmail.com>
2
8
3 * genutils.py(make_quoted_expr): Do not use Itpl, it does
9 * genutils.py(make_quoted_expr): Do not use Itpl, it does
General Comments 0
You need to be logged in to leave comments. Login now