0

I'm doing a Monte-Carlo simulation of a computer game which is basically the following:

void simulate(Game game)
{
  for (int i = 0; i < some_stuff)
  {
    Game game_copy = game;
    // change copy
    simulate(std::move(game_copy));
  }
}

Now, Game is full of vector<>s that are very expensive to copy.

What I found is that the above version is the fastest version in terms of avoiding copying, despite trying:

  • Taking the argument as Game&, const Game& and Game&&
  • Passing the argument with and without std::move

Can someone explain it to me? I don't understand how changing the argument type from pass-by-value to pass-by-reference suddenly reintroduces all those copying operations that I've managed to avoid. What's going on here? Is it the optimizing compiler that's at work?

Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
  • 1
    Possible duplicate of [What are move semantics?](http://stackoverflow.com/questions/3106110/what-are-move-semantics) – taskinoor Sep 28 '16 at 08:35
  • have you measured with this function or have you measured with some things left out? Here you don't do anything with the `Game` object or it's copies effectively – Hayt Sep 28 '16 at 08:36
  • 3
    not enough information – Richard Hodges Sep 28 '16 at 08:37
  • Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Leon Sep 28 '16 at 08:53
  • 1
    There can be multiple reasons. This can be due to a bug in the move constructor which skips some data thus shortening your simulation. It can be due to different optimizations because of differences in aliasing. Without an MCVE we can't tell for sure. – Leon Sep 28 '16 at 09:00

0 Answers0