I have profiled the performance between c++ vector and c-style array. The result is a little bit unexpected since the literature says the performance of vector should come very close to raw array, but it does not. Did I do anything wrong in my profiling?
void getVector1(int n)
{
if (n < 0)
{
throw std::invalid_argument(std::string("negative argument n:") + std::to_string(n));
}
auto tp1 = std::chrono::steady_clock::now();
std::vector<int> ivec(n);
int i = 0;
for (auto& x : ivec)
{
x = ++i;
}
auto tp2 = std::chrono::steady_clock::now();
std::chrono::duration<double, std::micro> dd = tp2 - tp1;
printf("spend %6.2f us time to create: %d elements vector inside %s() at %s:%d \n", dd.count(), n, __func__, __FILE__, __LINE__);
}
void getVector2(int n)
{
if (n < 0)
{
throw std::invalid_argument(std::string("negative argument n:") + std::to_string(n));
}
auto tp1 = std::chrono::steady_clock::now();
auto pvec = new int[n];
for (int i = 0; i < n; ++i)
{
pvec[i] = i;
}
auto tp2 = std::chrono::steady_clock::now();
std::chrono::duration<double, std::micro> dd = tp2 - tp1;
delete[] pvec;
printf("spend %6.2f us time to create: %d elements vector inside %s() at %s:%d \n", dd.count(), n, __func__, __FILE__, __LINE__);
}
int main()
{
int n = 10000000;
getVector1(n);
getVector2(n);
return 0;
}
The code was compiled using g++ with -O3 option.
spend 11946.38 us time to create: 10000000 elements vector inside getVector1() at testVectorSpeed.cpp
spend 7298.66 us time to create: 10000000 elements vector inside getVector2() at testVectorSpeed.cpp