Python faster than C++? How does this happen?
<p>I'm using Windows7 using CPython for python3.22 and MinGW's g++.exe for C++ (which means I use the libstdc++ as the runtime library). I wrote two simple programs to compare their speed.</p>

<p>Python:</p>

<pre><code>x=0
while x!=1000000:
    x+=1
    print(x)
</code></pre>

<p>C++:</p>

<pre><code>#include &lt;iostream&gt;
int main()
{
    int x=0;
    while(x!=1000000)
        {
        x++;
        std::cout&lt;&lt;x&lt;&lt;std::endl;
        }
    return 0;
}
</code></pre>

<p>Both not optimized.</p>

<p>I ran c++ first, then i ran python through the interactive command line, which is much slower than directly starting a .py file.</p>

<p>However, python outran c++ and turned out to be more than twice as fast. Python took 53 seconds, c++ took 1 minute and 54 seconds. </p>

<p>Is it because python has some special optimization done to the interpreter or is it because C++ has to refer to  and std which slows it down and makes it take up ram?<br>
Or is it some other reason?</p>

<p><strong>Edit:</strong> I tried again, with <code>\n</code> instead of <code>std::endl</code>, and compiling with the <code>-O3</code> flag, this time it took 1 min to reach 500,000.</p>
