New version of the effect of optimization, made in 2008 See the difference from the older version. ---------------- Case 1 new ---------------- main() { int i; for(i=1;i<=2;i++){printf("test out\n"); } printf("end\n"); } ----------------------- -O0 -S ------------------- .file "test-opt.c" .section .rodata .LC0: .string "test out\n" .LC1: .string "end\n" .text .p2align 2,,3 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp movl $1, -4(%ebp) .L2: cmpl $2, -4(%ebp) jg .L3 subl $12, %esp pushl $.LC0 call printf addl $16, %esp leal -4(%ebp), %eax incl (%eax) jmp .L2 .L3: subl $12, %esp pushl $.LC1 call printf addl $16, %esp leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ----------------------- -O3 -S ------------------- .file "test-opt.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "test out" .LC1: .string "end" .text .p2align 2,,3 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp pushl %ebx pushl %eax andl $-16, %esp subl $16, %esp movl $1, %ebx .L5: subl $12, %esp pushl $.LC0 call puts addl $16, %esp decl %ebx jns .L5 subl $12, %esp pushl $.LC1 call puts movl -4(%ebp), %ebx leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ---------------------- -Os -S minimum memory --------------------- .file "test-opt.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "test out" .LC1: .string "end" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp pushl %ebx movl $1, %ebx .L5: pushl $.LC0 call puts decl %ebx popl %eax jns .L5 pushl $.LC1 call puts movl -4(%ebp), %ebx leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ------------------------------------------------------ ----------------------------------------------------- Case 2 new ---------------- main() { int i,x; i=2; x=1+i; printf("result=%d\n",x); } ------------- -O0 -S ----- .file "test-opt2.c" .section .rodata .LC0: .string "result=%d\n" .text .p2align 2,,3 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax subl %eax, %esp movl $2, -4(%ebp) movl -4(%ebp), %eax incl %eax movl %eax, -8(%ebp) subl $8, %esp pushl -8(%ebp) pushl $.LC0 call printf addl $16, %esp leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ------------- -O3 -S ----- .file "test-opt2.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "result=%d\n" .text .p2align 2,,3 .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp subl $24, %esp pushl $3 pushl $.LC0 call printf leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ------------- -Os -S ----- .file "test-opt2.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "result=%d\n" .text .globl main .type main, @function main: pushl %ebp movl %esp, %ebp pushl $3 pushl $.LC0 call printf leave ret .size main, .-main .ident "GCC: (GNU) 3.4.6 [FreeBSD] 20060305" ------------------------------------------------ Case 3 ------------------------------------------------- main() { long i,x; x=0; for(i=1;i<100000000;i++){x=x+i; } printf("result=%d\n",x); } ------------------------------------------- inoue@kir.ics.es.osaka-u.ac.jp% gcc test-opt1.c inoue@kir.ics.es.osaka-u.ac.jp% time a.out result=887459712 0.382u 0.000s 0:00.38 100.0% 5+180k 0+0io 0pf+0w inoue@kir.ics.es.osaka-u.ac.jp% gcc -O3 test-opt1.c inoue@kir.ics.es.osaka-u.ac.jp% time a.out result=887459712 0.057u 0.000s 0:00.05 100.0% 5+184k 0+0io 0pf+0w inoue@kir.ics.es.osaka-u.ac.jp%