C++ 如何获得两个时间间隔之间的差异';使用c++;
我在做一个项目,我需要检查两次之间的时差,我得到的是毫秒的时差,但现在我想用微秒和纳秒的时差得到准确的时差 我的代码:C++ 如何获得两个时间间隔之间的差异';使用c++;,c++,qt,C++,Qt,我在做一个项目,我需要检查两次之间的时差,我得到的是毫秒的时差,但现在我想用微秒和纳秒的时差得到准确的时差 我的代码: QDateTime oStartTime = QDateTime::currentDateTime(); // some operetions //the difference is in micro and nano seconds differ QDateTime oEndTime = QDateTime::currentDateTime()
QDateTime oStartTime = QDateTime::currentDateTime();
// some operetions //the difference is in micro and nano seconds differ
QDateTime oEndTime = QDateTime::currentDateTime();
quint64 elapsed = oStartTime.daysTo(oEndTime)*1000*60*60*24 + oStartTime.time().msecsTo(oEndTime.time());
上面的代码只会给我毫秒的时间差。我还需要微纳秒。有没有关于如何使用微秒和纳秒获取时间的建议?QT提供了使用系统的高分辨率计数器获取时间差的功能(请参阅)。QT提供了使用系统的高分辨率计数器获取时间差的功能(请参阅)。C++11提供了处理时间的库。它有专门为计算时间间隔而设计的稳定时钟。还有duration模板类的微秒和纳秒实例化
#include <chrono> // C++11
#include <iostream>
using namespace std::chrono;
int main()
{
steady_clock::time_point tp1 = steady_clock::now();
std::cout<<"some time to spend..."<<std::endl;
steady_clock::time_point tp2 = steady_clock::now();
nanoseconds spent_time = duration_cast<nanoseconds>(tp2 - tp1);
std::cout<<"It took "<<spent_time.count()<<" nanoseconds."<<std::endl;
return 0;
}
C++11提供了与时间配合使用的库。它有专门为计算时间间隔而设计的稳定时钟。还有duration模板类的微秒和纳秒实例化
#include <chrono> // C++11
#include <iostream>
using namespace std::chrono;
int main()
{
steady_clock::time_point tp1 = steady_clock::now();
std::cout<<"some time to spend..."<<std::endl;
steady_clock::time_point tp2 = steady_clock::now();
nanoseconds spent_time = duration_cast<nanoseconds>(tp2 - tp1);
std::cout<<"It took "<<spent_time.count()<<" nanoseconds."<<std::endl;
return 0;
}
chrono,就是你要找的。chrono不支持我使用的QT4.3.2版本和visual studio 2005编译,因此可以获取micro和nano secondstry GetSystemTimeAsFileTime(LPFILETIME),其中FILETIME结构包含一个64位值,表示100纳秒时间戳的数量。std::chrono就是您要查看的。chrono不支持我使用QT4.3.2版本和visual studio 2005编译,因此可以获取micro和nano secondstry GetSystemTimeAsFileTime(LPFILETIME),其中FILETIME结构包含表示100纳秒时间戳数的64位值。但我的QT是4.3.2版本,它不支持QefersedTimer,那么,你能给我建议一些其他方法来获得微秒和纳秒的时间差吗?你可以使用本机计时器。请看。但是我的QT是4.3.2版本,它不支持QeReleastTimer,所以请您建议我任何其他方法来获得微秒和纳秒的时间差。您可以使用本机计时器。请参阅。我收到此类错误,似乎它不支持chrono致命错误C1083:无法打开包含文件:“chrono”:没有此类文件或directory@SrikanthTogara启用C++11使您的编译器能够访问
chrono
。我收到这种类型的错误,似乎不支持chrono致命错误C1083:无法打开包含文件:“chrono”:没有此类文件或directory@SrikanthTogara为您的编译器启用C++11以访问chrono
。