|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:5c0e4a56352cc8a60aa6e2ced2611bc186078af3c849647256f0c4a810004ee9" |
| 4 | + "signature": "sha256:b6e7356842906c447ef3c7520aac098e7fe4f888e46fdd5ae06905f1ebe68e27" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
48 | 48 | "- [`bool` is a subclass of `int`](#bool_int)\n", |
49 | 49 | "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", |
50 | 50 | "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", |
51 | | - "- [When immutable Tuples aren't so immutable](#immutable_tuple)" |
| 51 | + "- [When immutable Tuples aren't so immutable](#immutable_tuple)\n", |
| 52 | + "- [List comprehensions are fast, but generators are faster!?](#list_generator)" |
52 | 53 | ] |
53 | 54 | }, |
54 | 55 | { |
|
966 | 967 | "cell_type": "markdown", |
967 | 968 | "metadata": {}, |
968 | 969 | "source": [ |
969 | | - "## List comprehensions are fast, but generators are faster!\n" |
| 970 | + "<br>\n", |
| 971 | + "<br>\n", |
| 972 | + "<a name='list_generator'></a>\n", |
| 973 | + "\n", |
| 974 | + "## List comprehensions are fast, but generators are faster!?\n", |
| 975 | + "\n", |
| 976 | + "Not, really (or significantly, see the benchmarks below). So what's the reason to prefer one over the other?\n", |
| 977 | + "- use lists if you want to use list methods \n", |
| 978 | + "- use generators when you are dealing with huge collections to avoid memory issues" |
970 | 979 | ] |
971 | 980 | }, |
972 | 981 | { |
|
975 | 984 | "input": [ |
976 | 985 | "import timeit\n", |
977 | 986 | "\n", |
978 | | - "def plainlist():\n", |
| 987 | + "def plainlist(n=100000):\n", |
979 | 988 | " my_list = []\n", |
980 | | - " for i in range(1,1000000):\n", |
| 989 | + " for i in range(n):\n", |
981 | 990 | " if i % 5 == 0:\n", |
982 | 991 | " my_list.append(i)\n", |
983 | 992 | " return my_list\n", |
984 | 993 | "\n", |
985 | | - "def listcompr():\n", |
986 | | - " my_list = [i for i in range(1,1000000) if i % 5 == 0]\n", |
| 994 | + "def listcompr(n=100000):\n", |
| 995 | + " my_list = [i for i in range(n) if i % 5 == 0]\n", |
987 | 996 | " return my_list\n", |
988 | 997 | "\n", |
989 | | - "def generator():\n", |
990 | | - " my_gen = (i for i in range(1,1000000) if i % 5 == 0)\n", |
| 998 | + "def generator(n=100000):\n", |
| 999 | + " my_gen = (i for i in range(n) if i % 5 == 0)\n", |
991 | 1000 | " return my_gen\n", |
992 | 1001 | "\n", |
993 | | - "def generator_yield():\n", |
994 | | - " for i in range(1,1000000):\n", |
| 1002 | + "def generator_yield(n=100000):\n", |
| 1003 | + " for i in range(n):\n", |
995 | 1004 | " if i % 5 == 0:\n", |
996 | 1005 | " yield i" |
997 | 1006 | ], |
998 | 1007 | "language": "python", |
999 | 1008 | "metadata": {}, |
1000 | 1009 | "outputs": [], |
1001 | | - "prompt_number": 60 |
| 1010 | + "prompt_number": 75 |
1002 | 1011 | }, |
1003 | 1012 | { |
1004 | 1013 | "cell_type": "markdown", |
|
1027 | 1036 | " for i in listcompr:\n", |
1028 | 1037 | " print(i)\n", |
1029 | 1038 | "\n", |
1030 | | - "print('plain_list', end = '')\n", |
| 1039 | + "print('plain_list: ', end = '')\n", |
1031 | 1040 | "%timeit test_plainlist\n", |
| 1041 | + "print('\\nlistcompr: ', end = '')\n", |
1032 | 1042 | "%timeit test_listcompr\n", |
| 1043 | + "print('\\ngenerator: ', end = '')\n", |
1033 | 1044 | "%timeit test_generator\n", |
| 1045 | + "print('\\ngenerator_yield: ', end = '')\n", |
1034 | 1046 | "%timeit test_generator_yield" |
1035 | 1047 | ], |
1036 | 1048 | "language": "python", |
|
1040 | 1052 | "output_type": "stream", |
1041 | 1053 | "stream": "stdout", |
1042 | 1054 | "text": [ |
1043 | | - "plain_list" |
| 1055 | + "plain_list: 10000000 loops, best of 3: 26.2 ns per loop" |
1044 | 1056 | ] |
1045 | 1057 | }, |
1046 | 1058 | { |
1047 | | - "ename": "NameError", |
1048 | | - "evalue": "global name 'test_plainlist' is not defined", |
1049 | | - "output_type": "pyerr", |
1050 | | - "traceback": [ |
1051 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", |
1052 | | - "\u001b[0;32m<ipython-input-62-a2bedd84850f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'plain_list'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_plainlist'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 19\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_listcompr'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit test_generator'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
1053 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mmagic\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 2203\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2204\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2205\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2206\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2207\u001b[0m \u001b[0;31m#-------------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
1054 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_line_magic\u001b[0;34m(self, magic_name, line)\u001b[0m\n\u001b[1;32m 2124\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'local_ns'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2125\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2126\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2127\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2128\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
1055 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n", |
1056 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magic.py\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 193\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 194\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
1057 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 1011\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1012\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1013\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1014\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1015\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;34m*=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
1058 | | - "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/timeit.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 188\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 189\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 190\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 191\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
1059 | | - "\u001b[0;32m<magic-timeit>\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer)\u001b[0m\n", |
1060 | | - "\u001b[0;31mNameError\u001b[0m: global name 'test_plainlist' is not defined" |
| 1059 | + "output_type": "stream", |
| 1060 | + "stream": "stdout", |
| 1061 | + "text": [ |
| 1062 | + "\n", |
| 1063 | + "\n", |
| 1064 | + "listcompr: 10000000 loops, best of 3: 26.1 ns per loop" |
| 1065 | + ] |
| 1066 | + }, |
| 1067 | + { |
| 1068 | + "output_type": "stream", |
| 1069 | + "stream": "stdout", |
| 1070 | + "text": [ |
| 1071 | + "\n", |
| 1072 | + "\n", |
| 1073 | + "generator: 10000000 loops, best of 3: 25.9 ns per loop" |
| 1074 | + ] |
| 1075 | + }, |
| 1076 | + { |
| 1077 | + "output_type": "stream", |
| 1078 | + "stream": "stdout", |
| 1079 | + "text": [ |
| 1080 | + "\n", |
| 1081 | + "\n", |
| 1082 | + "generator_yield: 10000000 loops, best of 3: 26 ns per loop" |
| 1083 | + ] |
| 1084 | + }, |
| 1085 | + { |
| 1086 | + "output_type": "stream", |
| 1087 | + "stream": "stdout", |
| 1088 | + "text": [ |
| 1089 | + "\n" |
1061 | 1090 | ] |
1062 | 1091 | } |
1063 | 1092 | ], |
1064 | | - "prompt_number": 62 |
| 1093 | + "prompt_number": 76 |
1065 | 1094 | }, |
1066 | 1095 | { |
1067 | 1096 | "cell_type": "code", |
|
0 commit comments