3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

std::chronoで小数点のdurationを使う

Posted at

c++のプログラムの計算時間を計るのに、std::chrono を使っていたけど、std::chrono::millisecondsでは荒すぎるし、std::chrono::microsecondsでは、分散を求めるときにオーバーフローするし、困った。
調べてみると、std::chrono::duration::count()は小数点も返せるみたいなので、その方法をメモ。

小数のstd::chrono::duration型を作って使う

// float型で、1/1000秒単位の duration
using ms_f = std::chrono::duration<float, std::raito<1, 1000>>;

auto t0 = std::chrono::system_clock::now();
SomeProcessing();
auto t1 = std::chrono::system_clock::now();

// ミリ秒単位だけど、ミリ秒未満も切り捨てられない!
float dt = std::chrono::duration_cast<ms_f>(t1 - t0).count();

system_clockなので、ミリ秒以下の精度があるかどうかは別問題。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?