mardi 21 septembre 2021

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

I usually return an object of std::vector or std::map as an incoming reference paremeter(as funcVec2 and funcMap2 below). But it is a bit inconvenient when writing codes. So I think if I can use return value under c++11(as funcVec1 and funcMap1 below) because it will call move constructor but not copy constructor, so it maybe still spend only one construct time and no deconstruct as the form of incoming reference paremeter. But I write the codes below to verify it and it turns out that funcVec1 and funcMap1 takes more times then funcVec2 and funcMap2. So I am confused now why funcVec1 and funcMap1 takes so long?

#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;
}

Aucun commentaire:

Enregistrer un commentaire