On Tue, May 17, 2005 at 09:20:15PM +0200, Daniel Wunsch wrote:
On Tuesday 10 May 2005 21:18, Tim Starling wrote:
int main() {for (unsigned i=0;
i<(unsigned)1e9; i++);}
executes in 1.58 seconds on my desktop computer.
hm.. we all know how badly microbenchmarks can
go wrong. in this case i'd have expected the loop
to be optimized away, since it does not affect the
state of the rest of the program in any way..
It might be pretty off-topic, but not much more than the rest of this thread.
This loop is indeed optimized in any decent SSA-style compiler.
gcc3 experimentally supports SSA, and it's enabled by -fssa -fssa-dce -fssa-ccp
gcc4 is supposed to be totally SSA-based.
It is possible in non-SSA compilers too (there is no such thing as "the"
optimization
algorithm, every compiler has different bag of tricks), but they usually assume
that if something affects control flow (and the i variable does), it should not be
optimized away.
$ cat foo.cc
int main() {
for (unsigned i=0; i<(unsigned)1e9; i++);
return 0;
}
$ g++ --version
g++ (GCC) 3.3.6 (Debian 1:3.3.6-5)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -O6 foo.cc -o foo
$ time ./foo
real 0m1.882s
user 0m1.573s
sys 0m0.101s
$ g++ -fssa -fssa-dce -O6 foo.cc -o foo
$ time ./foo
real 0m0.006s
user 0m0.004s
sys 0m0.002s
$ g++ -fssa -fssa-dce -O6 foo.cc -S
$ cat foo.s
.file "foo.cc"
.text
.align 2
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB3:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
subl $8, %esp
.LCFI2:
andl $-16, %esp
xorl %eax, %eax
movl %ebp, %esp
popl %ebp
ret
.LFE3:
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.3.6 (Debian 1:3.3.6-5)"
$
Please adjust your expectations now. :-)