Thanks all for this very interresting discussion.
clanmjc, on my machine, I obtain :
Elapsed time with vectors : 10
Clocks Per/sec with vectors : 9.96
15000000000000
Elapsed time with arrays : 10
Clocks Per/Sec with arrays : 9.96
15000000000000 |
Here are my compiling flags :
-O3 -Wall -c -fmessage-length=0 -std=c++0x -std=gnu++0x
Now, if I modify TestObject to contain four integers :
1 2 3 4 5 6 7 8
|
struct TestObject
{
TestObject():int_type(1), int2(2), int3(3), int4(4) {}
int int_type;
int int2;
int int3;
int int4;
};
|
I obtain :
Elapsed time with vectors : 2
Clocks Per/sec with vectors : 2
15000000000000
Elapsed time with arrays : 0
Clocks Per/Sec with arrays : 0
15000000000000 |
And with 100x more loops, say with outer = 500000, inner = 100000 :
Elapsed time with vectors : 127
Clocks Per/sec with vectors : 126.86
15000000000000000
Elapsed time with arrays : 0
Clocks Per/Sec with arrays : 0
15000000000000000 |
And with 10000x more loops, say with outer = 5000000, inner = 1000000, it is the same. So I conclude the compiler makes too smart optimisation when code is useless.
Let's modify your code to use the data and avoid such optimisation :
1 2 3 4 5 6 7 8 9 10
|
for (int i=0;i<outer;i++) for (int j=0;j<inner;j++) {
vec1[0] = vec2[0];
vec1[1] = vec2[1];
vec1[2] = vec2[2];
sum+=vec1[0].int_type + i + j + vec1[0].int2 + vec1[1].int3 + vec1[2].int4;
vec2[1].int2 = vec1[2].int_type;
vec2[1].int4 = vec1[2].int3;
vec2[2].int2 = vec1[0].int_type;
vec2[2].int4 = vec1[0].int3;
}
|
1 2 3 4 5 6 7 8 9 10
|
for (int i=0;i<outer;i++) for (int j=0;j<inner;j++) {
arr1[0] = arr2[0];
arr1[1] = arr2[1];
arr1[2] = arr2[2];
sum+=arr1[0].int_type + i + j + arr1[0].int2 + arr1[1].int3 + arr1[2].int4;
arr2[1].int2 = arr1[2].int_type;
arr2[1].int4 = arr1[2].int3;
arr2[2].int2 = arr1[0].int_type;
arr2[2].int4 = arr1[0].int3;
}
|
Elapsed time with vectors : 5
Clocks Per/sec with vectors : 5.05
15004000000001
Elapsed time with arrays : 1
Clocks Per/Sec with arrays : 0.54
15004000000001 |
So with integers only, I obtain a gain of 10x times with arrays vs vectors !!! With -o2, I have the same result. With -o1,
arrays is identical,
vectors is worse : 7.17. With no optimisations at all, I get 25.75 for vectors vs 5.08 for arrays, 25.75/5.08=5.
As I will deal in my tables only with integers, as the size will not change nor the contents, and as my shared data are huge, I will stick to arrays.
vectors only have a plus when you use the dynamic sizing. In your case you can gladly use POD arrays |
Yep, thanks coder777