在C ++中获得准确的执行时间(微秒) [英] Getting an accurate execution time in C++ (micro seconds)

查看:276
本文介绍了在C ++中获得准确的执行时间(微秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个精确的执行时间在微秒的我用C ++实现的程序。
我试图获得与clock_t的执行时间,但它不准确。

I want to get an accurate execution time in micro seconds of my program implemented with C++. I have tried to get the execution time with clock_t but it's not accurate.

推荐答案

如果使用c ++ 11或更高版本,您可以使用 std :: chrono :: high_resolution_clock

If you are using c++11 or later you could use std::chrono::high_resolution_clock.

一个简单的用例:

auto start = std::chrono::high_resolution_clock::now();
...
auto elapsed = std::chrono::high_resolution_clock::now() - start;

long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();

此解决方案具有可移植的优点。

This solution has the advantage of being portable.

这篇关于在C ++中获得准确的执行时间(微秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆