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 120 |
|
4 | $Id: Magic.py 1205 2006-03-12 18:31:19Z vivainio $""" | |
5 |
|
5 | |||
6 | #***************************************************************************** |
|
6 | #***************************************************************************** | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
@@ -1560,6 +1560,107 b' Currently the magic system has the following functions:\\n"""' | |||||
1560 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1560 | self.shell.safe_execfile(f,self.shell.user_ns, | |
1561 | self.shell.user_ns,islog=1) |
|
1561 | self.shell.user_ns,islog=1) | |
1562 |
|
1562 | |||
|
1563 | def magic_timeit(self, parameter_s =''): | |||
|
1564 | """Time execution of a Python statement or expression | |||
|
1565 | ||||
|
1566 | Usage:\\ | |||
|
1567 | %timeit [-n<N> -r<R> [-t|-c]] statement | |||
|
1568 | ||||
|
1569 | Time execution of a Python statement or expression using the timeit | |||
|
1570 | module. | |||
|
1571 | ||||
|
1572 | Options: | |||
|
1573 | -n<N>: execute the given statement <N> times in a loop. If this value | |||
|
1574 | is not given, a fitting value is chosen. | |||
|
1575 | ||||
|
1576 | -r<R>: repeat the loop iteration <R> times and take the best result. | |||
|
1577 | Default: 3 | |||
|
1578 | ||||
|
1579 | -t: use time.time to measure the time, which is the default on Unix. | |||
|
1580 | This function measures wall time. | |||
|
1581 | ||||
|
1582 | -c: use time.clock to measure the time, which is the default on | |||
|
1583 | Windows and measures wall time. On Unix, resource.getrusage is used | |||
|
1584 | instead and returns the CPU user time. | |||
|
1585 | ||||
|
1586 | -p<P>: use a precision of <P> digits to display the timing result. | |||
|
1587 | Default: 3 | |||
|
1588 | ||||
|
1589 | ||||
|
1590 | Examples:\\ | |||
|
1591 | In [1]: %timeit pass | |||
|
1592 | 10000000 loops, best of 3: 53.3 ns per loop | |||
|
1593 | ||||
|
1594 | In [2]: u = None | |||
|
1595 | ||||
|
1596 | In [3]: %timeit u is None | |||
|
1597 | 10000000 loops, best of 3: 184 ns per loop | |||
|
1598 | ||||
|
1599 | In [4]: %timeit -r 4 u == None | |||
|
1600 | 1000000 loops, best of 4: 242 ns per loop | |||
|
1601 | ||||
|
1602 | In [5]: import time | |||
|
1603 | ||||
|
1604 | In [6]: %timeit -n1 time.sleep(2) | |||
|
1605 | 1 loops, best of 3: 2 s per loop | |||
|
1606 | ||||
|
1607 | ||||
|
1608 | The times reported by %timeit will be slightly higher than those reported | |||
|
1609 | by the timeit.py script when variables are accessed. This is due to the | |||
|
1610 | fact that %timeit executes the statement in the namespace of the shell, | |||
|
1611 | compared with timeit.py, which uses a single setup statement to import | |||
|
1612 | function or create variables. Generally, the bias does not matter as long | |||
|
1613 | as results from timeit.py are not mixed with those from %timeit.""" | |||
|
1614 | import timeit | |||
|
1615 | import math | |||
|
1616 | ||||
|
1617 | units = ["s", "ms", "\xc2\xb5s", "ns"] | |||
|
1618 | scaling = [1, 1e3, 1e6, 1e9] | |||
|
1619 | ||||
|
1620 | opts, stmt = self.parse_options(parameter_s,'n:r:tcp:') | |||
|
1621 | if stmt == "": | |||
|
1622 | return | |||
|
1623 | timefunc = timeit.default_timer | |||
|
1624 | number = int(getattr(opts, "n", 0)) | |||
|
1625 | repeat = int(getattr(opts, "r", timeit.default_repeat)) | |||
|
1626 | precision = int(getattr(opts, "p", 3)) | |||
|
1627 | if hasattr(opts, "t"): | |||
|
1628 | timefunc = time.time | |||
|
1629 | if hasattr(opts, "c"): | |||
|
1630 | timefunc = clock | |||
|
1631 | ||||
|
1632 | timer = timeit.Timer(timer=timefunc) | |||
|
1633 | # this code has tight coupling to the inner workings of timeit.Timer, | |||
|
1634 | # but is there a better way to achieve that the code stmt has access | |||
|
1635 | # to the shell namespace? | |||
|
1636 | ||||
|
1637 | src = timeit.template % {'stmt': timeit.reindent(stmt, 8), 'setup': "pass"} | |||
|
1638 | code = compile(src, "<magic-timeit>", "exec") | |||
|
1639 | ns = {} | |||
|
1640 | exec code in self.shell.user_ns, ns | |||
|
1641 | timer.inner = ns["inner"] | |||
|
1642 | ||||
|
1643 | if number == 0: | |||
|
1644 | # determine number so that 0.2 <= total time < 2.0 | |||
|
1645 | number = 1 | |||
|
1646 | for i in range(1, 10): | |||
|
1647 | number *= 10 | |||
|
1648 | if timer.timeit(number) >= 0.2: | |||
|
1649 | break | |||
|
1650 | ||||
|
1651 | best = min(timer.repeat(repeat, number)) / number | |||
|
1652 | ||||
|
1653 | if best > 0.0: | |||
|
1654 | order = min(-int(math.floor(math.log10(best)) // 3), 3) | |||
|
1655 | else: | |||
|
1656 | order = 3 | |||
|
1657 | print "%d loops, best of %d: %.*g %s per loop" % (number, repeat, precision, | |||
|
1658 | best * scaling[order], | |||
|
1659 | units[order]) | |||
|
1660 | ||||
|
1661 | ||||
|
1662 | ||||
|
1663 | ||||
1563 | def magic_time(self,parameter_s = ''): |
|
1664 | def magic_time(self,parameter_s = ''): | |
1564 | """Time execution of a Python statement or expression. |
|
1665 | """Time execution of a Python statement or expression. | |
1565 |
|
1666 |
@@ -1,3 +1,14 b'' | |||||
|
1 | 2006-03-12 Ville Vainio <vivainio@gmail.com> | |||
|
2 | ||||
|
3 | * Magic.py (magic_timeit): Added %timeit magic, contributed by | |||
|
4 | Torsten Marek. | |||
|
5 | ||||
|
6 | 2006-03-12 Fernando Perez <Fernando.Perez@colorado.edu> | |||
|
7 | ||||
|
8 | * IPython/Magic.py (magic_macro): fix so that the n1-n2 syntax for | |||
|
9 | line ranges works again. | |||
|
10 | ||||
|
11 | ||||
1 | 2006-03-12 Fernando Perez <Fernando.Perez@colorado.edu> |
|
12 | 2006-03-12 Fernando Perez <Fernando.Perez@colorado.edu> | |
2 |
|
13 | |||
3 | * IPython/Magic.py (magic_macro): fix so that the n1-n2 syntax for |
|
14 | * IPython/Magic.py (magic_macro): fix so that the n1-n2 syntax for |
General Comments 0
You need to be logged in to leave comments.
Login now