简介
chrono是C++的时间库,源于boost,在C++11中纳入了标准,
相比于过去常用的GetTickCount()计时,chrono具有更高的精度,据说能够到达纳秒级别
示例代码
#include <iostream>
#include <chrono>
#include <iomanip>
using namespace std;
void main()
{
auto start = chrono::high_resolution_clock::now(); //开始时间
double db = 0; //代码运行时间
for (int i = 0; i < 100000; i++)
{
db += i;
}
auto end = chrono::high_resolution_clock::now(); //结束时间
__int64 duration = (end - start).count();
cout << "程序运行时间:" << setprecision(10) << duration / 1000000000.0 << "s"
<< "; " << duration / 1000000.0 << "ms"
<< "; " << duration / 1000.0 << "us"
<< endl;
cin.get();
}
这里也附上基于GetTickCount()的计时方法
#include<iostream>
#include<Windows.h>
using namespace std;
int main()
{

本文介绍了C++标准库中的chrono模块,该模块提供比传统GetTickCount()更高精度的计时功能,可达纳秒级别。通过示例代码对比了两种计时方法的差异,并解析了chrono库的具体使用方式。
最低0.47元/天 解锁文章
1117





