0
0

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 3 years have passed since last update.

C++ 処理時間測定 std::chrono::duration_cast

Posted at

以下のstopwatch()を処理したい前後でコールする。

# include <iostream>
# include <chrono>

float stopwatch(std::chrono::system_clock::time_point t = std::chrono::system_clock::now())
{
    static std::chrono::system_clock::time_point ref_time;
    if (ref_time == std::chrono::system_clock::time_point())
    {
        ref_time = t;
        return 0.0;
    }
    float elapsed = static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(t - ref_time).count());
    ref_time = t;
    return elapsed;
}

int main(int argc, char *argv[])
{
    stopwatch();
    // 時間測定したい処理
    std::cout << "process time: " << stopwatch() << "[ms]" << std::endl;
    return 0;
}
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?