help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: About g++ optimization


From: Andre Poenitz
Subject: Re: About g++ optimization
Date: Sun, 2 Apr 2006 11:40:05 +0200

rmansfield@gmail.com <rmansfield@gmail.com> wrote:
> Say for a minute, that f(n) returned a constant and IPCP could replace
> the f(n) with a constant, say 5.
> 
> For example:
> 
> for (int i = 5; i--; )
> 
> 
> On x86, gcc would generate for your example:
> 
>        movl  $4, %ebx
> L1:
>        decl    %ebx
>        .... ; loop body
>        cmpl    $-1, %ebx
>        jne     L1

Funny. Last time I looked there was no cmpl in this case.
Indeed, 3.3.1 produces for

int main()
{
        int i;
        int s = 0;
        for (i = 5; --i; ) 
                s += i;
        return s;
}

the following output (for -O3)


        .file   "1.c"
        .text
        .p2align 4,,15
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        movl    $4, %edx
        pushl   %eax
        pushl   %eax
        andl    $-16, %esp
        xorl    %eax, %eax
        .p2align 4,,7
.L6:
        addl    %edx, %eax
        decl    %edx
        jne     .L6
        leave
        ret
        .size   main, .-main
        .ident  "GCC: (GNU) 3.3.1 (SuSE Linux)"

So, no 'cmpl', and the loop body is just an add, a dec, and a jump.
As tight as it can get (without unrolling)

What version are you using?

Aehm, wait. Did you use '--i' or 'i--'? You need the latter (both to be
correct and to achieve the savings)

> where mine would be:
>        xorl    %ebx, %ebx
> L1:
>        incl    %ebx
>        .... ; loop body
>        cmpl    $5, %ebx
>        jne     L1
> 
> 'Technically', my code would be faster because zeroing out ebx is
> cheaper than a move. :)
> 
> Anyways, my point is that I avoid doing 'micro' optimizations because
> it does not always make the code faster, and it usually makes the code
> slightly harder to read.

I tend not to do micro optimizations myself as well - unless I think I
know what am doing.

Andre'

PS:

        for (i = n; i--; ) 

is about 25% less typing than

        for (i = 0; i != n; ++i)

and feels as 'idiomatic' as the latter after a while.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]