0

我在想类似的东西:

// Runs algorithm() n times during an amount of seconds.
void run(int seconds) {
  clock_t start = clock();
  clock_t current = start;
  while(double (current - start) / CLOCKS_PER_SEC <= seconds) {
    algorithm();
    current = clock();
  }   
}

我选择clock()time()在进程休眠时避免计费时间。我想知道是否有更好的方法来实现这一点,而不使用chrono

4

1 回答 1

2

STLSoft 有一个在 UNIX 和 windows 平台上工作的performance_counter 。该库只是标题,只需要一个简单的包含:

#include <platformstl/performance/performance_counter.hpp>

void run(int seconds) {
  platformstl::performance_counter pc;
  platformstl::performance_counter::interval_type current = 0;
  pc.start();
  while ((current / 1000) <= seconds){
    algorithm();
    current += pc.stop_get_milliseconds_and_restart();
  }
}

您还可以查看有关制作自己的提示的来源。

于 2012-10-03T01:35:51.620 回答