M
mark
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?
#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?