Hi,
i made a unusual discovery today. I wanted to compare the speed of a program using array notation with the speed of a program without it. The program adds two arrays of 1,000,000 integers one thousand times and then reports the result (see below).
int main(int argc, char ** argv) {
int array1[1000000];
int array2[1000000];
int a = 1;
for (int i = 0; i < 1000000; i++) {
array1[i] = a;
array2[i] = a;
}
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 1000000; i++) {
array1[i] += array2[i]; }
}
cout << array1[0]<< endl;
The second program is its array-notation equivalent
int main(int argc, char ** argv) {
int array1[1000000];
int array2[1000000];
array1[0:1000000] = a;
array2[0:1000000] = a;
for (int j = 0; j < 1000; j++) {
array1[0:1000000] += array2[0:1000000];
}
cout << array1[0]<< endl;
Without any optimization upon compilation, the first program (without the array-notation) took about 2.9 seconds and the second program took about 1.9 seconds. If i use optimization level -O3, the first program takes 0.6 seconds and the second program still takes 1.2 second. This is double the time the first program uses.
Obviously, the two for-loops of the first program get vectorized, when using the -O3 option. Still, i would have thought, that the array-notation version was at least as fast. Did i miss something there? Maybe there is a bug somewhere when trying to optimize array-notation-code, so it generates less efficient code.If not - what do have to do to get the array-notation as fast as the "normal" code?
I used a recent version of the cilkplus-branch of the gcc (a few days old) and a Core i7.



