The code below shows my test cases. I compiled both with clang++ --std=c++11 -O2 and with g++ --std=c++11 -O2.
long long *ary = new long long[100000000]();
for (long long i = 0; i < 100000000; ++i)
ary[i] = i;
std::vector<long long> vec(100000000, 0);
for (long long i = 0; i < 100000000; ++i)
vec[i] = i;
For both I did tests with the initialization only, and then the initialization and the for loop. The results are below:
GCC:
- Array initialization only: 0.182s
- Array initialization and for loop: 0.250s
- Vector initialization only: 0.169s
- Vector initialization and for loop: 0.252
Clang:
- Array initialization only: 0.004s
- Array initialization and for loop: 0.004s
- Vector initialization only: 0.150
- Vector initialization and for loop: 0.240s
The gcc results coincide with the common belief that vectors are as fast as arrays. Moreover, the clang and gcc results for vector pretty much agree. However the clang results are ridiculous, with the array performing considerably faster. Anyone have any idea why this is?