为什么std :: vector比数组慢? [英] Why is std::vector slower than an array?

查看:318
本文介绍了为什么std :: vector比数组慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下程序(启用优化功能)时,使用std::vectorfor循环大约需要0.04秒,而使用数组的for循环则需要0.0001秒.

When I run the following program (with optimization on), the for loop with the std::vector takes about 0.04 seconds while the for loop with the array takes 0.0001 seconds.

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

int main()
{
    int len = 800000;
    int* Data = new int[len];

    int arr[3] = { 255, 0, 0 };
    std::vector<int> vec = { 255, 0, 0 };

    auto start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < len; i++) {
        Data[i] = vec[0];
    }
    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = finish - start;
    std::cout << "The vector took " << elapsed.count() << "seconds\n";

    start = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < len; i++) {
        Data[i] = arr[0];
    }
    finish = std::chrono::high_resolution_clock::now();
    elapsed = finish - start;
    std::cout << "The array took " << elapsed.count() << "seconds \n";

    char s;
    std::cin >> s;
    delete[] Data;
}

该代码是我在编写raycaster时遇到的性能问题的简化版本. len变量对应于原始程序中的循环需要运行多少次(400像素* 400像素* 50最大渲染距离).由于复杂的原因(也许我不太了解如何使用数组),我必须在实际的raycaster中使用向量而不是数组.但是,正如该程序演示的那样,每秒只能给我20帧,而使用数组应该给我的每秒10,000帧令人羡慕(显然,这只是一个简化的性能测试).但是,不管这些数字有多准确,我仍然想尽可能地提高帧速率.那么,为什么矢量的执行速度比数组慢得多?有没有办法加快速度?谢谢你的帮助.如果我还有其他奇怪的事情可能会影响性能,请告诉我.在研究此问题的答案之前,我什至不了解优化,因此,如果还有更多类似的事情可能会提高性能,请告诉我(如果您解释了这些设置在哪里,我会更愿意.属性管理器而不是命令行,因为我还不知道如何使用命令行)

The code is a simplified version of a performance issue I was having while writing a raycaster. The len variable corresponds to how many times the loop in the original program needs to run (400 pixels * 400 pixels * 50 maximum render distance). For complicated reasons (perhaps that I don't fully understand how to use arrays) I have to use a vector rather than an array in the actual raycaster. However, as this program demonstrates, that would only give me 20 frames per second as opposed to the envied 10,000 frames per second that using an array would supposedly give me (obviously, this is just a simplified performance test). But regardless of how accurate those numbers are, I still want to boost my frame rate as much as possible. So, why is the vector performing so much slower than the array? Is there a way to speed it up? Thanks for your help. And if there's anything else I'm doing weirdly that might be affecting performance, please let me know. I didn't even know about optimization until researching an answer for this question, so if there are any more things like that which might boost the performance, please let me know (and I'd prefer if you explained where those settings are in the properties manager rather than command line since I don't yet know how to use the command line)

推荐答案

让我们观察 GCC如何优化此测试程序:

#include <vector>

int main()
{
    int len = 800000;
    int* Data = new int[len];

    int arr[3] = { 255, 0, 0 };
    std::vector<int> vec = { 255, 0, 0 };

    for (int i = 0; i < len; i++) {
        Data[i] = vec[0];
    }
    for (int i = 0; i < len; i++) {
        Data[i] = arr[0];
    }
    delete[] Data;
}

编译器正确地注意到向量是常数,然后将其消除.对于两个循环,生成的代码完全相同.因此,第一个循环使用数组还是向量应该无关紧要.

The compiler rightly notices that the vector is constant, and eliminates it. Exactly same code is generated for both loops. Therefore it should be irrelevant whether the first loop uses array or vector.

.L2:
    movups  XMMWORD PTR [rcx], xmm0
    add     rcx, 16
    cmp     rsi, rcx
    jne     .L2

使测试程序与众不同的是循环的顺序.注释指出在第三个循环添加到开头时,两个循环都需要在同一时间.

What makes difference in your test program is the order of loops. The comments point out that when a third loop is added to the beginning, both loops take the same time.

我希望使用现代编译器,在启用优化和禁用调试的情况下,访问向量的速度几乎与访问数组一样快.如果您的实际程序中有明显的差异,则问题出在其他地方.

I would expect that with a modern compiler accessing a vector would be approximately as fast as accessing an array, when optimization is enabled and debug is disabled. If there is an observable difference in your actual program, the problem lies somewhere else.

这篇关于为什么std :: vector比数组慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆