增加c++中的时间分辨率
Increase resolution of time in c++
我正在编写一段代码,它将告诉我执行"代码块"所花费的时间。对于这个问题,我的方法是创建timer_t
的两个实例,在开始执行该"代码块"之前给第一个实例当前时间,然后给第二个实例当前时间,当它完成执行。然后取两个实例的差值来计算运行时间。代码看起来像这样:(Visual Studio使用)
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<Windows.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time(NULL));
for(int i=0;i<10;i++)
{
cout<<rand()%100<<endl;
}
cout<<endl;
time_t timernow,timerthen;
timernow=time(NULL);
Sleep(300);
timerthen=time(NULL);
cout<<"Elapsed time: "<<timerthen-timernow;
_getch();
return 0;
}
但问题是,它给我的时间只有几秒(0在这种情况下,而不是0.3),但我想要的时间毫秒左右。有可能提高时间的分辨率/精度吗?谢谢你。
使用<chrono>
。具有高精度时钟和duration_cast
等时间测量设备。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- 日历时间,纳秒分辨率为纳秒纪元
- 高分辨率程序时间
- 如何获得高分辨率时间戳作为双精度
- 将高分辨率时钟时间转换为整数(Chrono)
- C++11如何打印出高分辨率时钟时间点
- 增加c++中的时间分辨率
- C++,时间函数(Linux)-纳秒分辨率
- 如何将C++提升posix_time::p时间的分辨率更改为毫秒?