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()しました。chronotime()を使用せずに、これを達成するためのより良い方法があるかどうかを知りたいです。

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 に答える