ultra-fast loop unrolling with g++ -O3

Discussion in 'C Programming' started by mark, Jun 12, 2008.

  1. mark

    mark Guest

    Why does the following excerpt of trivial code execute so quickly?

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]){
    static const int SIZE = 1000000;
    long nops = 0;
    int i, j;
    long int outer = atol(argv[1]);
    for(i=0; i < outer; i++){
    for(j=0; j < SIZE; j++){
    ++nops;
    // arr[j] = arr[j] + 1;
    } //for j
    } //for i
    printf("ran %ld ops\n", nops);
    } //main

    I compiled this with g++ -O3.
    When ran with 50000000000 as an argument, the nops variable is updated
    50000000000000000 times. Including loop logic this should take
    forever on my 2ghz computer. Yet it runs instantly. I used input from
    the command line so that nops simply isn't pre-calculated.

    This came about when trying to speed-test C arrays with C++ vectors;
    originally the code had an array-update line in the center of the
    loops. The vector version was crawling versus the C array (both
    compiled with -O3).

    What compile/hardware magic is going on, and is it possible to speed
    up the vector with it?
     
    mark, Jun 12, 2008
    #1
    1. Advertising

  2. mark schrieb:

    > What compile/hardware magic is going on, and is it possible to speed
    > up the vector with it?


    1. Your loop is optimized away.
    2. Probably not, unless you use vectory which don't do anything useful.
    Then yes.

    Regards,
    Johannes

    --
    "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
    reicht zu wissen, daß andere es besser können und andere es auch
    besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
    in de.sci.electronics <47fa8447$0$11545$>
     
    Johannes Bauer, Jun 12, 2008
    #2
    1. Advertising

  3. mark

    santosh Guest

    mark wrote:

    > Why does the following excerpt of trivial code execute so quickly?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > int main(int argc, char *argv[]){
    > static const int SIZE = 1000000;


    Better to use long to be safe.

    > long nops = 0;


    unsigned long will give you even more range.

    > int i, j;
    > long int outer = atol(argv[1]);
    > for(i=0; i < outer; i++){


    What if 'i' overflows. Make 'i' and 'j' unsigned long or long.

    > for(j=0; j < SIZE; j++){
    > ++nops;
    > // arr[j] = arr[j] + 1;
    > } //for j
    > } //for i
    > printf("ran %ld ops\n", nops);
    > } //main
    >
    > I compiled this with g++ -O3.
    > When ran with 50000000000 as an argument, the nops variable is updated
    > 50000000000000000 times. Including loop logic this should take
    > forever on my 2ghz computer. Yet it runs instantly. I used input from
    > the command line so that nops simply isn't pre-calculated.


    The compiler has optimised the loop away. It simply computes SIZE *
    outer and assigns the product to nops. The loop will be left untouched
    if you qualify nops with volatile.

    > What compile/hardware magic is going on, and is it possible to speed
    > up the vector with it?


    You'll have to ask in comp.lang.c++.
     
    santosh, Jun 12, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Edwards
    Replies:
    5
    Views:
    386
    Thomas Matthews
    Jul 7, 2003
  2. =?ISO-8859-1?Q?Per_Nordl=F6w?=

    g++ loop unrolling performance

    =?ISO-8859-1?Q?Per_Nordl=F6w?=, Aug 31, 2004, in forum: C++
    Replies:
    1
    Views:
    1,073
    Jack Klein
    Sep 1, 2004
  3. Richard Cavell

    Unrolling a loop

    Richard Cavell, Feb 23, 2005, in forum: C++
    Replies:
    3
    Views:
    2,255
    Phillip Jordan
    Feb 23, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    printf ultra-fast question

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 14, 2006, in forum: C Programming
    Replies:
    7
    Views:
    380
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Feb 15, 2006
  5. V

    unrolling nested for-loop

    V, May 10, 2008, in forum: C Programming
    Replies:
    10
    Views:
    1,112
    Willem
    May 10, 2008
Loading...

Share This Page