c++11 使用 std::map 作为 return 值

c++11 use std::map as return value

我通常return一个std::vector或std::map的对象作为传入的参考参数(如下面的funcVec2和funcMap2)。但是写代码的时候有点不方便。所以我想如果我可以在 c++11 下使用 return 值(如下面的 funcVec1 和 funcMap1),因为它会调用移动构造函数而不是复制构造函数,所以它可能仍然只花费一个构造时间而不是解构输入参考参数的形式。 但是我写了下面的代码来验证它,结果发现 funcVec1 和 funcMap1 比 funcVec2 和 funcMap2 花费更多的时间。所以我现在很困惑为什么 funcVec1 和 funcMap1 需要这么长时间?

#include <iostream>
#include <vector>
#include <map>
#include <chrono>
using namespace std;
vector<int> funcVec1() {
    vector<int >vec;
    for (int i = 0; i < 10; ++i) {
        vec.push_back(i);
    }
    return vec;
}

void funcVec2(vector<int>&vec) {
    for (int i = 0; i < 10; ++i) {
        vec.push_back(i);
    }
    return;
}

map<int, int> funcMap1() {
    map<int, int>tmpMap;
    for (int i = 0; i < 10; ++i) {
        tmpMap[i] = i;
    }
    return tmpMap;
}

void funcMap2(map<int, int>&tmpMap) {
    for (int i = 0; i < 10; ++i) {
        tmpMap[i] = i;
    }
}

int main()
{
    using namespace std::chrono;
    system_clock::time_point t1 = system_clock::now();
    for (int i = 0; i < 100000; ++i) {
        vector<int> vec1 = funcVec1();
    }
    auto t2 = std::chrono::system_clock::now();
    cout << "return vec takes " << (t2 - t1).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t2 - t1).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    vector<int> vec2;
    for (int i = 0; i < 100000; ++i) {
        funcVec2(vec2);
    }
    auto t3 = system_clock::now();
    cout << "reference vec takes " << (t3 - t2).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t3 - t2).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    for (int i = 0; i < 100000; ++i) {
        map<int, int> tmpMap1 = funcMap1();
    }
    auto t4 = system_clock::now();
    cout << "return map takes " << (t4 - t3).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t4 - t3).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    map<int, int>tmpMap2;
    for (int i = 0; i < 100000; ++i) {
        funcMap2(tmpMap2);
    }
    auto t5 = system_clock::now();
    cout << "reference map takes " << (t5 - t4).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t5 - t4).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    return 0;
}
  1. 您不仅在测量操作时间,还包括打印输出。这是次优的。

  2. 您应该在发布模式下测量性能。请注意,您没有对您的对象做任何有用的事情,优化器可能会丢弃您想要测量的大部分代码。

  3. 比较不“公平”。例如,在您的 map1 案例中,您正在构建一个空地图,填充它(此处发生内存分配),然后将其丢弃。在 map2 的情况下,您一遍又一遍地重复使用相同的地图对象。您没有一遍又一遍地分配内存。