简体   繁体   English

std :: vector vs std :: array performance

[英]std::vector vs std::array performance

I was looking at the new chrono library (C++11) and trying to use it. 我正在查看新的计时库(C ++ 11)并尝试使用它。 I wrote the two following programs: 我写了以下两个程序:

vector.cpp vector.cpp

#include <iostream>
#include <vector>
#include <chrono>

int main()
{
    std::vector<double> vector(1000000, 0.);

    auto start = std::chrono::high_resolution_clock::now();
    for(int i(0); i < vector.size(); i++)
    {
        vector[i] += 1.;
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::cout << "Elapsed time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl;

    return 0;
}

array.cpp array.cpp

#include <iostream>
#include <array>
#include <algorithm>
#include <chrono>

int main()
{
    std::array<double, 1000000> array;

    std::fill(array.begin(), array.end(), 0.);

    auto start = std::chrono::high_resolution_clock::now();
    for(int i(0); i < array.size(); i++)
    {
        array[i] += 1.;
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::cout << "Elapsed time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl;

    return 0;
}

I obtained 9 millisecond for the array program and 12 milliseconds for the vector program. 我为阵列程序获得了9毫秒,为矢量程序获得了12毫秒。 The std::vector seems about 33% slower than the std::array. std :: vector似乎比std :: array慢约33%。 I'm doing it right? 我做得对吗? Why this difference? 为何如此区别?

Ps: I'm using GCC 4.7, Mac OS X 10.7. Ps:我正在使用GCC 4.7,Mac OS X 10.7。

g++-mp-4.7 -std=c++11 vector.cpp -o vector
g++-mp-4.7 -std=c++11 array.cpp -o array

I changed your code to this: 我把你的代码更改为:

std::array<double, 1000000> array;

double total = 0;
std::fill(array.begin(), array.end(), 0.);

for (unsigned j = 0; j < 1000; ++j)
{
    auto start = std::chrono::high_resolution_clock::now();

    for (unsigned i = 0; i < array.size(); i++)
    {
        array[i] += 1.;
    }

    auto end = std::chrono::high_resolution_clock::now();
    total = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}

std::cout << total << " for Array." << std::endl;

std::vector<double> vector(1000000, 0.);
total = 0;

for (unsigned j = 0; j < 1000; ++j)
{
    auto start = std::chrono::high_resolution_clock::now();

    for (unsigned i = 0; i < vector.size(); i++)
    {
        vector[i] += 1.;
    }

    auto end = std::chrono::high_resolution_clock::now();
    total = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}

std::cout << total << " for Vector." << std::endl;

My results using -O3 : 我的结果使用-O3

8123 for Array.
8117 for Vector.

Seems to me that both are equally fast. 在我看来两者都同样快。

The numbers are meaningless without enabling optimizations. 没有启用优化,这些数字毫无意义。 Most likely the repeated calls to size() make the difference in your case. 最有可能重复调用size()会对你的情况产生影响。

A std::array has a size known at compile time, so the memory will most likely be allocated on the stack. std::array在编译时具有已知的大小,因此内存很可能在堆栈上分配。

A std::vector uses std::allocator (which probably uses `new to allocate memory from the free store (aka the heap) at runtime). std::vector使用std::allocator (可能在运行时使用`new来从免费存储(也就是堆)中分配内存)。

I would say 30% is normal for heap vs stack allocation. 我想说30%对于堆与堆栈分配是正常的。


EDIT: Running this a couple of times (not the most scientific measurement, I know) on liveworkspace.org ( std::vector and std::array ), I get 8 vs 10 ms. 编辑:在liveworkspace.org( std::vectorstd::array )上运行这几次(不是最科学的测量),我得到8 vs 10 ms。 As all allocations are indeed outside the measurement, I would naively conclude that accessing the heap is slower than accessing stack memory. 由于所有分配确实都在测量范围之外,我天真地得出结论,访问堆比访问堆栈内存要慢。 I wouldn't be surprised if this is generally true, as there is an extra indirection in the case of the heap. 如果这通常是正确的,我不会感到惊讶,因为在堆的情况下存在额外的间接性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM