Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The compiler is even smarter than that. :)</p> <p>In fact, it realizes that you aren't using the result of the loop. So it took out the entire loop completely!</p> <p>This is called <a href="http://en.wikipedia.org/wiki/Dead_code_elimination" rel="noreferrer">Dead Code Elimination</a>.</p> <p>A better test is to print the result:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { int i; int count = 0; for(i = 0; i &lt; 2000000000; i++){ count = count + 1; } // Print result to prevent Dead Code Elimination printf("%d\n", count); } </code></pre> <p><strong>EDIT :</strong> I've added the required <code>#include &lt;stdio.h&gt;</code>; the MSVC assembly listing corresponds to a version without the <code>#include</code>, but it should be the same.</p> <hr> <p>I don't have GCC in front of me at the moment, since I'm booted into Windows. But here's the disassembly of the version with the <code>printf()</code> on MSVC:</p> <p><strong>EDIT : I had the wrong assembly output. Here's the correct one.</strong></p> <pre><code>; 57 : int main(){ $LN8: sub rsp, 40 ; 00000028H ; 58 : ; 59 : ; 60 : int i; int count = 0; ; 61 : for(i = 0; i &lt; 2000000000; i++){ ; 62 : count = count + 1; ; 63 : } ; 64 : ; 65 : // Print result to prevent Dead Code Elimination ; 66 : printf("%d\n",count); lea rcx, OFFSET FLAT:??_C@_03PMGGPEJJ@?$CFd?6?$AA@ mov edx, 2000000000 ; 77359400H call QWORD PTR __imp_printf ; 67 : ; 68 : ; 69 : ; 70 : ; 71 : return 0; xor eax, eax ; 72 : } add rsp, 40 ; 00000028H ret 0 </code></pre> <p>So yes, Visual Studio does this optimization. I'd assume GCC probably does too.</p> <p>And yes, GCC performs a similar optimization. Here's an assembly listing for the same program with <code>gcc -S -O2 test.c</code> (gcc 4.5.2, Ubuntu 11.10, x86):</p> <pre><code> .file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "%d\n" .text .p2align 4,,15 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp andl $-16, %esp subl $16, %esp movl $2000000000, 8(%esp) movl $.LC0, 4(%esp) movl $1, (%esp) call __printf_chk leave ret .size main, .-main .ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2" .section .note.GNU-stack,"",@progbits </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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