【问题标题】:Dangled with std::mt19937 and profiling the code with gprof与 std::mt19937 挂钩并使用 gprof 分析代码
【发布时间】:2014-11-06 10:20:50
【问题描述】:

我的 .h 文件中有这段代码:

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

但我从不致电random()

当我使用 gprof 分析代码时,我得到:

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)

发生了什么?

【问题讨论】:

  • return 0;std::this_thread::sleep_for(std::chrono::seconds(1)); return 0;替换函数体是否还会出现?
  • 如果您使用 -O3 编译器会积极内联所有内容,因此如果 rng 可以随机使用某些代码,它可能会混淆 rng 和 random 的代码。使用 -O0 重试
  • @Surt 就是这样! stefan,由于其他解决方案有效,我没有测试你的解决方案。 Surt,你可以回答这个问题,这样我就可以接受了。

标签: c++11 random profiling gprof mersenne-twister


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2013-07-16
    • 1970-01-01
    • 2019-03-17
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多