我已经分析了c ++ vector和c-style数组之间的性能.结果有点出乎意料,因为文献说矢量的性能应该非常接近原始阵列,但事实并非如此.我在剖析中做错了什么?
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 …Run Code Online (Sandbox Code Playgroud)