mardi 16 juin 2020

std::accumulate C++20 version

I'm trying to understand this code but I can't figure out why this version

for (; first != last; ++first) 
    init = std::move(init) + *first;

is faster than this

for (; first != last; ++first)
    init += *first;

I did take them from std::accumulate. The assembly code of the first version is longer than the second one. Even if the first version create an rvalue ref of init, it always create a temp value by adding *first and then assign it to init, that is the same process in second case where it create a temp value and then assign it to init. So, why using std::move is better than "append value" with the += operator?

Aucun commentaire:

Enregistrer un commentaire