title: CPython: Why does a 3-line script require far more than 3 cycles in the interpreter to execute?
body: <p>I just watched <a href="https://www.youtube.com/watch?time_continue=1&amp;v=LhadeL7_EIU" rel="nofollow noreferrer">this Youtube lecture</a> about CPython Internals by Philip Guo, I am puzzled at one thing.</p>
  
  <p>At 25:55, he modifies the C source of CPython by inserting <code>printf(“hello\n”)</code> at the start of the endless loop which runs all the byte code instructions; you can do the same by:</p>
  
  <ul>
  <li>Downloading the Python 2.7 C source code</li>
  <li>Open the file <code>Python/ceval.c</code></li>
  <li>Find the start of the endless evaluation loop, <code>for (;;) {</code></li>
  <li>Add the line <code>printf('hello\n');</code> as the first line of the endless loop.</li>
  <li>Run <code>configure</code> and <code>make</code> to build the Python binary.</li>
  </ul>
  
  <p>He writes a 3 line test.py:</p>
  
  <pre><code>X = 1
  Y = 2
  print X + Y
  </code></pre>
  
  <p>The puzzle is, when he runs test.py with the modified interpreter, how come there are so many “hello” before we see “3”? </p>
  
  <p>That 3 line code should compile to just a few byte code instructions, load value 1, load value 2 and the instruction to call print, so I would imagine when it comes to execute the byte code compiled from test.py, we should see just a few "hello".</p>
  
  <p>So the compiler actually generates many internal byte code instructions before compiling the external Python script?</p>