optimizers are overrated
- From: "copx" <copx@xxxxxxxxx>
- Date: Wed, 23 Apr 2008 01:38:18 +0200
Optimizers are overrated
I started learning ASM not long ago to improve my understanding of the
hardware architecture and my ability to optimize C code. The results of my
first experiment were surprising to say at least. After reading the chapter
on loops in my ASM book I wanted to test whether modern C compilers are
actually as smart as commonly claimed. I chose a most simple loop: calling
putchar 100 times. The first function (foo) uses a typical C style loop to
test the assumption that "the compiler will optimize that better than any
human could". The second function (bar) is based my newly gained knowledge,
the loop is basically ASM written in C. Now I am certain if I asked here
which one is more efficient all you guys would reply "the compiler will most
likely generate the same code in both cases" (I have read such claims
countless times here). Well, look at the ASM output below to see how wrong
your assumption is.
/* C Code */
void foo(void)
{
int i;
for (i = 0; i < 100; i++) putchar('a');
}
void bar(void)
{
int i = 100;
do {
putchar('a');
} while (--i);
}
As I said, damn simple. No nasty side effects, no access to global
variables, etc. The optimizer has no excuses. It should generate optimial
code in both cases. But see the result:
/* GCC 4.3.0 on x86/Windows, -O2 */
/* foo */
L7:
subl $12, %esp
pushl $97
call _putchar
incl %ebx
addl $16, %esp
cmpl $100, %ebx
jne L7
/* bar */
L2:
subl $12, %esp
pushl $97
call _putchar
addl $16, %esp
decl %ebx
jne L2
Comment: See, even the most recent version of the probably most widely used
compiler can not correctly optimize a most simple loop! At least GCC
understood the bar loop, so my "write C like ASM" optimization worked.
At this point you might wonder what horrible things an average C compiler
will do when GCC already fails so badly. Here is the gruesome result:
/* lccwin32, optimize on */
/* foo */
_$4:
pushl $97
call _putchar
popl %ecx
incl %edi
cmpl $100,%edi
jl _$4
/* bar */
_$10:
pushl $97
call _putchar
popl %ecx
movl %edi,%eax
decl %eax
movl %eax,%edi
or %eax,%eax
jne _$10
Comment: lcc is unable to optimize the loop just like GCC, but it adds
insults to injury by actually generating worse code for the ASM-style loop!
So you cannot even optimize the loop yourself!
/* MS Visual C++ 6 /O2 */
For this compiler I had to replace the putchar call with a call to a custom
my_putchar function otherwise the compiler replaces the putchar calls with
direct OS API stuff. While this is a good optimization it is not the
subject of this test, and only makes the resulting asm harder to read, so I
supressed that.
/* foo */
jmp SHORT $L833
$L834:
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
$L833:
cmp DWORD PTR _i$[ebp], 100
jge SHORT $L835
push 97
call _my_putchar
add esp, 4
jmp SHORT $L834
$L835:
/* bar */
$L840:
push 97
call _my_putchar
add esp, 4
mov eax, DWORD PTR _i$[ebp]
sub eax, 1
mov DWORD PTR _i$[ebp], eax
cmp DWORD PTR _i$[ebp], 0
jne SHORT $L840
Comment: Amazingly enough, this compiler has found yet another way to screw
up. Would you have thought that each compiler generates different code for
such a simple construct?
I hope you agree that the compiler of the beast deserves the award "Worst of
Show" for this mess. Are MS compilers still this bad?
.
- Follow-Ups:
- Re: optimizers are overrated
- From: Flash Gordon
- Re: optimizers are overrated
- From: Anonymous
- Re: optimizers are overrated
- From: Walter Banks
- Re: optimizers are overrated
- From: Bartc
- Re: optimizers are overrated
- From: Paul Brettschneider
- Re: optimizers are overrated
- From: user923005
- Re: optimizers are overrated
- From: A. Sinan Unur
- Re: optimizers are overrated
- From: robertwessel2@xxxxxxxxx
- Re: optimizers are overrated
- From: Ian Collins
- Re: optimizers are overrated
- Prev by Date: Re: a fanny question
- Next by Date: Re: how to specify power of number
- Previous by thread: Wholesale AAA Tag Heuer Aquaracer Diamonds Mens Watch WAF111D.BA0810 Replica
- Next by thread: Re: optimizers are overrated
- Index(es):
Relevant Pages
|