edo1z blog

プログラミングなどに関するブログです

C++ clock

C++で時間計測したいときはclock()が使えます。

参考:clock

コードサンプル

#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;

int main() {
  cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << endl;
  clock_t start = clock();
  cout << "start: " << start << endl;
  sleep(2);
  clock_t end = clock();
  cout << "end: " << end << endl;
  printf("%.4f sec\n", (double)(end - start) / CLOCKS_PER_SEC);
}

結果

CLOCKS_PER_SEC: 1000
start: 3
end: 2005
2.0020 sec