Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(In what follows I'm assuming you're using Python 3; in Python 2 the story would be similar but we'd have to discuss the cost of memory allocation due to <code>range</code>.)</p> <p>Your second program spends its time running <em>in Python bytecode</em>. Let's disassemble it using the <a href="http://docs.python.org/3/library/dis.html" rel="nofollow"><code>dis</code></a> module:</p> <pre><code>&gt;&gt;&gt; import dis &gt;&gt;&gt; dis.dis('a=[]\nfor i in range(0,60000000):\n a.append(0)') 1 0 BUILD_LIST 0 3 STORE_NAME 0 (a) 2 6 SETUP_LOOP 36 (to 45) 9 LOAD_NAME 1 (range) 12 LOAD_CONST 0 (0) 15 LOAD_CONST 1 (60000000) 18 CALL_FUNCTION 2 (2 positional, 0 keyword pair) 21 GET_ITER &gt;&gt; 22 FOR_ITER 19 (to 44) 25 STORE_NAME 2 (i) 3 28 LOAD_NAME 0 (a) 31 LOAD_ATTR 3 (append) 34 LOAD_CONST 0 (0) 37 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 40 POP_TOP 41 JUMP_ABSOLUTE 22 &gt;&gt; 44 POP_BLOCK &gt;&gt; 45 LOAD_CONST 2 (None) 48 RETURN_VALUE </code></pre> <p>The loop runs from bytes 22 to 41, and each time round the loop, Python has to decode and execute seven byte code instructions, and call a function too (the function being <code>a.append</code>). That's 420 million byte code instructions and 60 million function calls.</p> <p>Whereas your first program spends its time running <em>in native code</em>:</p> <pre><code>&gt;&gt;&gt; dis.dis('a = [0] * 60000000') 1 0 LOAD_CONST 0 (0) 3 BUILD_LIST 1 6 LOAD_CONST 1 (60000000) 9 BINARY_MULTIPLY 10 STORE_NAME 0 (a) 13 LOAD_CONST 2 (None) 16 RETURN_VALUE </code></pre> <p>You can see that not only is there no loop, there are no function calls. All the work happens "under the hood" inside the <code>BINARY_MULTIPLY</code> instruction, which dispatches to <a href="http://hg.python.org/cpython/file/ae9b74c2b357/Objects/listobject.c#l509" rel="nofollow"><code>list_multiply</code> in <code>listobject.c</code></a>, and since the list <code>[0]</code> contains just one element, the result is constructed in this tight loop at lines 529–536:</p> <pre class="lang-c prettyprint-override"><code>if (Py_SIZE(a) == 1) { elem = a-&gt;ob_item[0]; for (i = 0; i &lt; n; i++) { items[i] = elem; Py_INCREF(elem); } return (PyObject *) np; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload