- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
[cpp] int main(int argc, char ** argv) {
int array1[1000000];
int array2[1000000];
int a = 1;
for (int i = 0; i < 1000000; i++) {
array1 = a;
array2 = a;
}
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 1000000; i++) {
array1 += array2; }
}
cout << array1[0]<< endl; [/cpp]
The second program is its array-notation equivalent
[cpp] 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;
[/cpp]
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page