LoginSignup
105
94

More than 5 years have passed since last update.

C++でフリープラットフォームな時間計測

Last updated at Posted at 2015-07-28

c++で処理時間等を計測する際,clock()やtimeGetTime(), GetTickCount()等を用いていたものの,
clock()は精度が10msec程度(処理系依存)なため使い勝手があまり良くなく,GetTickCountは精度は良いもののwinmm.lib(windows系)で用意されている関数のためxcode等で使えない.

std::chrono

一方std::choronoはプラットフォームに依存せず,使い勝手もよい(精度は1msec程度の模様).
使い方は


#include <iostream>
#include <chrono>

int main(){
 std::chrono::system_clock::time_point  start, end; // 型は auto で可
 start = std::chrono::system_clock::now(); // 計測開始時間
// 処理
 end = std::chrono::system_clock::now();  // 計測終了時間
 double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count(); //処理に要した時間をミリ秒に変換

}

となっている.
std::chrono::duration_cast<変換型>(duration)の変換型には,ナノ秒(nanoseconds),マイクロ秒(microseconds),ミリ秒(microseconds),秒(seconds) を指定できる.

105
94
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
105
94