dimanche 18 avril 2021

Why is std::copy from a global vector much slower than a local vector?

Consider following:

#include <vector>
#include <algorithm>

const std::vector<int> global(100, 42);

static void copy_from_global() {
  std::vector<int> v(global.size());
  std::copy(
    global.begin(), global.end(), 
    v.begin()
  );
}

static void copy_from_local()
{
  const std::vector<int> local(100, 42);
  std::vector<int> v(local.size());
  std::copy(
    local.begin(), local.end(), 
    v.begin()
  );
}

In the case of using libc++'s std::copy, copy_from_global will be 6000 times slower than copy_from_local. What is the reason for the difference? You can see the benchmark here: https://quick-bench.com/q/2D49o7Rvdp6P7klHro2fkgfaouk

Aucun commentaire:

Enregistrer un commentaire