简体   繁体   English

C++ - Google 基准测试停止计时 function

[英]C++ - Google benchmark stop timing function

I'm trying to exclude the conversion of string into an object in a function.我试图在 function 中排除字符串到 object 的转换。 This is the involved function:这是涉及到的function:

std::vector<std::pair<value_type, size_t>> read_file(const std::string path, benchmark::State& state) {
  
  std::string kmer;
  std::vector<std::pair<value_type, size_t>> data;
  
  std::ifstream file(path);
  while (std::getline(file, kmer)) {
    state.PauseTiming();
    kmer_t tmp(kmer);
    state.ResumeTiming();
    data.push_back(std::make_pair(tmp.value, tmp.index));
  }

  return data;
}

The function scope is to read a file and convert line by line into an object. function scope是读取文件,逐行转换成object。 The resulted object is insered into a vector of pair.将得到的 object 插入到一对向量中。 I include in my project the google benchmark library to compute how much time and memory is used.我在我的项目中包含了google benchmark库来计算使用了多少时间和 memory。 I would like to exclude the conversion from the total count.我想从总数中排除转换。 I implemented the function just like the documentation said but the resulting time is much higher then a normal computation without the timer management.就像文档说的那样,我实现了 function,但是结果时间比没有计时器管理的正常计算要高得多。

I also found this old but related opened issue but I can't resolve my problem.我还发现了这个旧但相关的未解决问题,但我无法解决我的问题。 How can I fix this problem or there are any work around for the issue?我该如何解决这个问题,或者有任何解决这个问题的方法?

There is nothing you can do here, this is expected.您在这里无能为力,这是意料之中的。 Starting and stopping a timer requires some form of synchronization with the OS, and the overhead of that in your case seems to be much higher than creating the temporary object.启动和停止计时器需要与操作系统进行某种形式的同步,并且在您的情况下,这样做的开销似乎比创建临时 object 高得多。

However, this shouldn't be an issue.但是,这应该不是问题。 If you're trying to compare several methods of filling the vector and exclude the creation of the objects in all of them then the overhead of stopping and restarting the timer will be the same for all of these different methods, so if one of the methods is faster than another it will also be faster with the added timer management overhead, only the relative difference will be smaller.如果您尝试比较几种填充向量的方法并排除在所有这些方法中创建对象,那么停止和重新启动计时器的开销对于所有这些不同的方法都是相同的,所以如果其中一种方法比另一个更快,增加定时器管理开销也会更快,只是相对差异会更小。

I'd even argue that in your case where you're measuring the entire parsing of the file and aren't microbenchmarking eg push_back vs emplace_back it is better to include the object creation in the measurements to get a more accurate sense of how significant performance differences are, eg when comparing this version with one reusing the kmer_t object between iterations (and thus possibly reusing the already allocated memory for data members).我什至认为,在您测量文件的整个解析而不是微基准测试(例如push_backemplace_back )的情况下,最好将 object 创建包含在测量中,以更准确地了解性能的重要性差异是,例如,当将此版本与在迭代之间重用kmer_t object 的版本进行比较时(因此可能重用已分配的 memory 用于数据成员)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM