1

I have a vector of vectors, and I want to connect them one by one to form a long vector. This could be done by inserting at the end. Inspired by this question, I was thinking that using make_move_iterator would replace copy with move and thus would be more efficient. But the following test demonstrates that make_move_iterator will cause a larger time consumption.

#include <iostream>
#include <string>
#include <vector>
#include <chrono>
using namespace std;

int main()
{
    string a = "veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongstring";
    vector<string> b(10,a);
    vector<vector<string> > c(1000,b);
    vector<string> d,e;

    auto t1 = chrono::system_clock::now();
    for(auto& item : c)
    {
        d.insert(d.end(),item.begin(),item.end());
    }
    cout << c[0][0].length() << endl;

    auto t2 = chrono::system_clock::now();
    for(auto& item:c)
    {
        e.insert(e.end(), std::make_move_iterator(item.begin()),std::make_move_iterator(item.end()));
    }

    auto t3 = chrono::system_clock::now();    
    cout << chrono::duration_cast<chrono::nanoseconds>(t2-t1).count() << endl;
    cout << chrono::duration_cast<chrono::nanoseconds>(t3-t2).count() << endl;
    cout << c[0][0].length()  << endl;
    cout << "To check that c has been moved from." <<endl;
}

//Output:
//122
//1212000
//1630000
//0
//To check that c has been moved from.

Thus I'm wondering, does this approach really help improve efficiency?

Community
  • 1
  • 1
Jing Li
  • 639
  • 7
  • 18
  • Cannot reproduce, on my system the moving version is about twice as fast as the copying version. – Kerrek SB Mar 03 '15 at 18:56
  • @KerrekSB Thank you. Yes I realized that the online compiler I use may be outdated. I will stick to ideone in the future. – Jing Li Mar 03 '15 at 19:01

1 Answers1

0

The test in the question description was conducted on cpp shell

I later tried on ideone and it turned out that make_move_iterator is obviously more efficient. So it seems to be a compiler-dependent thing.

122
320576
98434
0
To check that c has been moved from.
Jing Li
  • 639
  • 7
  • 18