如何输出高精度时间差
来源:互联网 发布:mac ps2017怎么破解 编辑:程序博客网 时间:2024/06/06 13:15
昨天看核心编程看到171页,看到了GetThreadTimes,结果按书上的源码运行无法获取时间向师傅求助,师傅告诉我
猪头三 22:25:44你看到的时间一样,是因为你的 时间精度不够猪头三 22:25:56你应该要更加高精度的时间获取于是搜到到这篇
http://zhidao.baidu.com/question/85502612.html#281447-qzone-1-49353-2b4b15c6dbd92b17940ffe005c9ace27请教VC++高手 如何输出高精度时间差文中高手回答到
晕了,想钱想疯了也就算了,百度的分数也嫌多嫌少的,开了眼了.楼主到MSDN里看下这两个函数:
BOOL QueryPerformanceFrequency(LARGE_INTEGER* lpFrequency);BOOL QueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount);
另外给你个实现类:
class CStopwatch {public:CStopwatch() { QueryPerformanceFrequency(&m_liPerfFreq); Start();}void Start() { QueryPerformanceCounter(&m_liPerfStart); }__int64 Now() const {LARGE_INTEGER liPerfNow;QueryPerformanceCounter(&liPerfNow);return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);}private:LARGE_INTEGER m_liPerfFreq;LARGE_INTEGER m_liPerfStart;};
在Now函数里,有个乘数是1000,目的是让Now返回的时间为毫秒。使用:
CStopwatch stopwatch;//do the work that would take long time;__int64 qwElapsedTime = stopwatch.Now();//qwElapsedTime indicates how long the work has spent;
注意,因为是高精度时间,因此,你要做的工作应该是短期内能完成的工作,时间太长,误差会特别大,因为线程有占用时间片的因素存在。测试成功获取代码运行时间我的程序代码如下
#include <stdio.h>#include <tchar.h>#include <Windows.h>#include <Locale.h>class CStopwatch {public:CStopwatch() { QueryPerformanceFrequency(&m_liPerfFreq); Start(); }void Start() { QueryPerformanceCounter(&m_liPerfStart); }__int64 Now() const {LARGE_INTEGER liPerfNow;QueryPerformanceCounter(&liPerfNow);return (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);}private:LARGE_INTEGER m_liPerfFreq;LARGE_INTEGER m_liPerfStart;};void PerformLongOperation();int _tmain(int argc, _TCHAR* argv[]){_wsetlocale(LC_ALL, L"chs"); //本地化PerformLongOperation();return 0;}void PerformLongOperation(){CStopwatch stopwatch;//执行运算代码Sleep(5000);__int64 qwElapsedTime = stopwatch.Now();_tprintf(TEXT("总执行时间%lld\n"),qwElapsedTime);}这段高精度代码原来是核心编程里的,,,
0 0
- 如何输出高精度时间差
- 高精度时间差计算代码
- 获取高精度时间差
- c++处理高精度时间差
- C++中获取高精度时间差
- C++中获取高精度时间差
- php输出时间差
- 标准格式输出时间差
- 如何将PPT输出为高精度的图片
- PHP输出时间差函数代码
- Android如何计算时间差
- Android如何计算时间差
- python 中如何计算时间差...
- Oracle中如何计算时间差
- Oracle中如何计算时间差
- Oracle中如何计算时间差
- ORACLE中如何计算时间差
- Oracle中如何计算时间差
- 收藏5篇文章
- 收藏的关于开发的一些东西
- 反驳--(知乎)大龄门外汉如何进入软件开发行业?的回复
- 一段挂起进程中所有线程的代码
- GetThreadTimes获取其它线程cpu时间
- 如何输出高精度时间差
- 注意!GetThreadPriority的返回值不是系统的优先级值
- windows核心编程 第8章201页旋转锁的代码在新版Visual Studio运行问题
- FILETIME类型到LARGE_INTEGER类型的转换
- windows如何利用计划任务自动关机?
- visual c++.net 技术内幕 第6版 附带的程序如何在vs2013中编译成功
- c#学习路线应该靠谱
- postgresql 常用命令
- Android实现GPS定位功能