lundi 26 septembre 2022

How std::move works in improving performance?

I am learning std::move.

I think this keyword can avoid copy in real code, to improve performance.

but when i write the demo code:

#include <bits/stdc++.h>
#include "util/time_util.h"
using namespace std;

int main() {
  util::time_util::Timer t;
  t.StartTimer();
  std::vector<std::string> a;
  for (size_t i = 0; i < 1e7; ++i) {
    std::string s = "asd";
    // a.push_back(std::move(s));  // this is a right value, can avoid copy, i think
    a.push_back(s);  // this is a left value, will copy
  }
  t.EndTimer("cost");
}

As above shows, i test two kind of push_back, one is push_back left value, one is push_back std::move.

but i found the cost time is same(I dont use any compiler optimization).

could you help on this, and could you list a demo code, when std::move will improve performance significantly?

Aucun commentaire:

Enregistrer un commentaire