0

I have in my .h file this code:

std::mt19937 rng(time(NULL)); // mersenne numbers
int random(int n) {
  std::uniform_int_distribution<int> distribution(0, n);
  return distribution(rng);
}

but I never call random().

When I am profiling the code with gprof, I get:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 99.13      2.28     2.28        1     2.28     2.28  random(int)

What is happening?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Does it still appear if you replace the function body with a `return 0;` or a `std::this_thread::sleep_for(std::chrono::seconds(1)); return 0;`? – stefan Sep 12 '14 at 10:08
  • If your using -O3 the compiler aggressively inline everything, so it might mix up the code for rng and random if rng could use some of the code in random. Try again using -O0 – Surt Sep 12 '14 at 10:56
  • @Surt that's it! stefan, since the other solution worked, I didn't test yours. Surt, you can answer the question, so that I can accept it. – gsamaras Sep 12 '14 at 15:29

1 Answers1

1

If your using -O3 the compiler aggressively inline everything, so it might mix up the code for rng and random if rng could use some of the code in random. Try again using -O0

Surt
  • 15,501
  • 3
  • 23
  • 39