samedi 26 août 2017

Why does the copy constructor for std::string appear to be faster than its move counterpart?

I am trying to learn move semantics and I read that a move would likely be faster than a copy. However, I see quite the opposite for the following trivial code:

for (int i = 0; i < 100000000; ++i) {
    std::string a("Copy");
    std::string b = a;
}

for (int i = 0; i < 100000000; ++i) {
    std::string a("Move");
    std::string b = std::move(a);
}

And here's the time it takes on my mac:

$ time ./copy.out
real    0m2.511s
user    0m2.481s
sys     0m0.011s


$ time ./move.out
real    0m3.993s
user    0m3.933s
sys     0m0.020s

Aucun commentaire:

Enregistrer un commentaire