简体   繁体   English

考虑干净代码的性能,哪个更好

[英]Performance with clean code in mind, what is better

For the cleanness of a for loop I like the range-based for loop. 为了使for循环更简洁,我喜欢基于范围的for循环。 Inside the for loop I want to fill a vector of which I know the size before hand, but I am missing an index value. 在for循环中,我想填充一个矢量,该矢量我之前已经知道大小,但是我缺少索引值。 I now have two methods of doing it, declare a vector and add elements with push_back or create an initialized vector (should be faster right since no allocation is needed?), calculate the index and insert an element. 我现在有两种方法,声明一个向量,并使用push_back添加元素或创建一个初始化的向量(因为不需要分配,应该更快一些吗?),计算索引并插入一个元素。

Question: What is the performance drawback of one over the other and/or are there better implementations. 问题:一个相对于另一个的性能缺点是什么和/或是否有更好的实现。

Below the example code for this, the real code contains a relative small number of items (less than 10 presumably) but it will run through this procedure millions of times. 在此示例代码之下,实际代码包含相对较少的项目(大概少于10个),但是它将在此过程中运行数百万次。

    //The vector as input for the for loop
    std::vector<double> vecIn = { 1, 2, 3, 4, 5 };

    //Adding values to vectors, vector size changes on each loop right?
    std::vector<double> vecOut1;
    //Loop through vector with range looping
    for (auto& val : vecIn) {
        vecOut1.push_back(val); //In reality val is some calculated value based on the input.
    }

    //Adding values to initialized vector, but need to calculate index.
    std::vector<double> vecOut2(vecIn.size());
    //Loop through vector with range looping
    for (auto& val : vecIn) {
        auto i = &val - &vecIn[0];
        vecOut2[i] = val;  //In reality val is some calculated value based on the input.
    }

I like the first loop for the shortness, but a afraid that preformance wise is will be worse. 我喜欢简短的第一个循环,但担心执行明智会更糟。

Of course I can also declare an index at the beginning of the loop and iterate it but that seems to defeat the purpose of cleanness a bit. 当然,我也可以在循环开始时声明一个索引并对其进行迭代,但这似乎有点违背了清理的目的。

EDIT: To clarify, this is demo code where I copy one vector into another. 编辑:澄清一下,这是演示代码,我将一个向量复制到另一个向量中。 In the real program the input vector is processed and new values are calculated based on the input vector. 在实际程序中,处理输入向量,并根据输入向量计算新值。 The new values need to be inserted/appended to the vector output. 新值需要插入/附加到向量输出中。 In the real code the input is not even a vector but a boost::ublas::matrix. 在实际代码中,输入甚至不是向量,而是boost :: ublas :: matrix。

It is pretty much the same. 几乎一样。

You can let the compiler optimize things for you by doing: 您可以通过以下方式让编译器为您优化内容:

std::vector<double> vecOut1(vecIn.begin(), vecIn.end());

or 要么

std::vector<double> vecOut1 = vecIn

By doing this you are copying vecIn to vecOut1. 这样,您可以将vecIn复制到vecOut1。

Another advise: Whether you can tell the type you are dealing with in for loops, avoid the use of keyword auto , and specify the type. 另一个建议是:是否可以在for循环中确定要处理的类型,请避免使用关键字auto ,并指定类型。

EDIT: As OP question was unclear, here is a new answer. 编辑:由于OP问题尚不清楚,这是一个新答案。

Your first aproach is just fine. 您的第一个方法就很好。 There are some good alternatives too. 也有一些不错的选择。

If you know the size of your vecOut2 vector before inserting elements you can have index accessing in a better way: 如果在插入元素之前知道vecOut2向量的大小,则可以采用更好的方式进行索引访问:

std::vector<double> vecOut2(vecIn.size());
for(int i = 0;i < vecOut2.size(); i++){
   vecOut2[i] = vecIn[i] + /* Your calculation */;
}

This really depends on how do you want to perform your calculation, and if the calculation is linear (if you have to jump between indexes or not). 这实际上取决于您要如何执行计算,以及计算是否是线性的(是否必须在索引之间跳转)。

All your first loop needs is a .reserve() to play in the same performance order of magnitude as the second one: 您的第一个循环所需的全部就是一个.reserve() ,以与第二个循环相同的性能数量级播放:

std::vector<double> vecOut1;
vecOut1.reserve(vecIn.size());
for (auto& val : vecIn) 
    vecOut1.push_back(val); 

reserve preallocates the requested elements without changing the vectors size - so there's no reallocation going on in the loop. reserve在不更改向量size情况下预分配所请求的元素-因此循环中没有重新分配。

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

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