C++11 挂起std::mt19937并使用gprof分析代码

C++11 挂起std::mt19937并使用gprof分析代码,c++11,random,profiling,gprof,mersenne-twister,C++11,Random,Profiling,Gprof,Mersenne Twister,我的.h文件中有以下代码: std::mt19937 rng(time(NULL)); // mersenne numbers int random(int n) { std::uniform_int_distribution<int> distribution(0, n); return distribution(rng); } 发生了什么?如果您使用-O3,编译器会积极地内联所有内容,因此如果rng可以随机使用一些代码,那么它可能会混淆rng和random的代码。使用-

我的.h文件中有以下代码:

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

发生了什么?

如果您使用-O3,编译器会积极地内联所有内容,因此如果rng可以随机使用一些代码,那么它可能会混淆rng和random的代码。使用-O0重试

如果将函数体替换为
返回0,它是否仍然出现或a
std::this_thread::sleep_for(std::chrono::seconds(1));返回0?如果您使用-O3,编译器会积极地内联所有内容,因此如果rng可以随机使用一些代码,那么它可能会混淆rng和random的代码。请使用重试-O0@Surt就这样!stefan,因为另一个解决方案有效,我没有测试你的。苏尔特,你可以回答这个问题,这样我就可以接受了。
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)