|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:ab74fdc9e8ae58388d1fc428599abc86a3d03938d0f51769cef38ab4028d516a" |
| 4 | + "signature": "sha256:d5895f75b2ac58db150d7b521682366a447ffb2fb0b7db7e551edd40e6d1ab10" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
66 | 66 | " - [Adding elements to a dictionary](#adding_dict_elements)\n", |
67 | 67 | "- [Comprehensions vs. for-loops](#comprehensions)\n", |
68 | 68 | "- [Copying files by searching directory trees](#find_copy)\n", |
69 | | - "- [Returning column vectors slicing through a numpy array](#row_vectors)" |
| 69 | + "- [Returning column vectors slicing through a numpy array](#row_vectors)\n", |
| 70 | + "- [Speed of numpy functions vs Python built-ins and std. lib.](#numpy)" |
70 | 71 | ] |
71 | 72 | }, |
72 | 73 | { |
|
1691 | 1692 | ], |
1692 | 1693 | "prompt_number": 91 |
1693 | 1694 | }, |
| 1695 | + { |
| 1696 | + "cell_type": "markdown", |
| 1697 | + "metadata": {}, |
| 1698 | + "source": [ |
| 1699 | + "<a name='numpy'></a>\n", |
| 1700 | + "<br>\n", |
| 1701 | + "<br>\n" |
| 1702 | + ] |
| 1703 | + }, |
| 1704 | + { |
| 1705 | + "cell_type": "markdown", |
| 1706 | + "metadata": {}, |
| 1707 | + "source": [ |
| 1708 | + "# Speed of numpy functions vs Python built-ins and std. lib." |
| 1709 | + ] |
| 1710 | + }, |
| 1711 | + { |
| 1712 | + "cell_type": "code", |
| 1713 | + "collapsed": false, |
| 1714 | + "input": [ |
| 1715 | + "import numpy as np\n", |
| 1716 | + "import timeit\n", |
| 1717 | + "\n", |
| 1718 | + "samples = list(range(1000000))\n", |
| 1719 | + "\n", |
| 1720 | + "%timeit(sum(samples))\n", |
| 1721 | + "%timeit(np.sum(samples))" |
| 1722 | + ], |
| 1723 | + "language": "python", |
| 1724 | + "metadata": {}, |
| 1725 | + "outputs": [ |
| 1726 | + { |
| 1727 | + "output_type": "stream", |
| 1728 | + "stream": "stdout", |
| 1729 | + "text": [ |
| 1730 | + "100 loops, best of 3: 18.3 ms per loop\n", |
| 1731 | + "10 loops, best of 3: 136 ms per loop" |
| 1732 | + ] |
| 1733 | + }, |
| 1734 | + { |
| 1735 | + "output_type": "stream", |
| 1736 | + "stream": "stdout", |
| 1737 | + "text": [ |
| 1738 | + "\n" |
| 1739 | + ] |
| 1740 | + } |
| 1741 | + ], |
| 1742 | + "prompt_number": 6 |
| 1743 | + }, |
| 1744 | + { |
| 1745 | + "cell_type": "code", |
| 1746 | + "collapsed": false, |
| 1747 | + "input": [ |
| 1748 | + "%timeit(list(range(1000000)))\n", |
| 1749 | + "%timeit(np.arange(1000000))\n", |
| 1750 | + "\n", |
| 1751 | + "# note that in Python range() is implemented as xrange()\n", |
| 1752 | + "# with lazy evaluation (generator)" |
| 1753 | + ], |
| 1754 | + "language": "python", |
| 1755 | + "metadata": {}, |
| 1756 | + "outputs": [ |
| 1757 | + { |
| 1758 | + "output_type": "stream", |
| 1759 | + "stream": "stdout", |
| 1760 | + "text": [ |
| 1761 | + "10 loops, best of 3: 82.6 ms per loop\n", |
| 1762 | + "100 loops, best of 3: 5.35 ms per loop" |
| 1763 | + ] |
| 1764 | + }, |
| 1765 | + { |
| 1766 | + "output_type": "stream", |
| 1767 | + "stream": "stdout", |
| 1768 | + "text": [ |
| 1769 | + "\n" |
| 1770 | + ] |
| 1771 | + } |
| 1772 | + ], |
| 1773 | + "prompt_number": 11 |
| 1774 | + }, |
| 1775 | + { |
| 1776 | + "cell_type": "code", |
| 1777 | + "collapsed": false, |
| 1778 | + "input": [ |
| 1779 | + "import statistics\n", |
| 1780 | + "\n", |
| 1781 | + "%timeit(statistics.mean(samples))\n", |
| 1782 | + "%timeit(np.mean(samples))" |
| 1783 | + ], |
| 1784 | + "language": "python", |
| 1785 | + "metadata": {}, |
| 1786 | + "outputs": [ |
| 1787 | + { |
| 1788 | + "output_type": "stream", |
| 1789 | + "stream": "stdout", |
| 1790 | + "text": [ |
| 1791 | + "1 loops, best of 3: 1.14 s per loop\n", |
| 1792 | + "10 loops, best of 3: 141 ms per loop" |
| 1793 | + ] |
| 1794 | + }, |
| 1795 | + { |
| 1796 | + "output_type": "stream", |
| 1797 | + "stream": "stdout", |
| 1798 | + "text": [ |
| 1799 | + "\n" |
| 1800 | + ] |
| 1801 | + } |
| 1802 | + ], |
| 1803 | + "prompt_number": 14 |
| 1804 | + }, |
1694 | 1805 | { |
1695 | 1806 | "cell_type": "code", |
1696 | 1807 | "collapsed": false, |
|
0 commit comments