Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ultra-fast loop unrolling with g++ -O3

Reply
Thread Tools

ultra-fast loop unrolling with g++ -O3

 
 
mark
Guest
Posts: n/a
 
      06-12-2008
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?

 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      06-12-2008
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$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      06-12-2008
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++.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
unrolling nested for-loop V C Programming 10 05-10-2008 12:25 PM
Unrolling a loop Richard Cavell C++ 3 02-23-2005 02:45 PM
g++ loop unrolling performance =?ISO-8859-1?Q?Per_Nordl=F6w?= C++ 1 09-01-2004 01:52 AM
Loop unrolling question. Does this make sense? John Edwards C++ 5 07-07-2003 02:37 PM



Advertisments