用C++获得精确的时间(纳秒级)

hhhhhhio110 2012-11-12 06:41:52
如题,网上搜了一下,用QueryPerformanceFrequency()这个函数,但是我用VS确找不到这个函数,用MSDN也查不到,这是为什么,有没有其他的一些方法呢
...全文
1329 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-11-13
  • 打赏
  • 举报
回复
听说gettimeofday到微秒.
mujiok2003 2012-11-13
  • 打赏
  • 举报
回复 1

#include <cstdio>
#include <Windows.h>
int main()
{
	LARGE_INTEGER freq, start, end;
	QueryPerformanceFrequency(&freq);
	QueryPerformanceCounter(&start);
	Sleep(10000); // 10 sec
	QueryPerformanceCounter(&end);
	printf("%f  sec\n", (end.QuadPart - start.QuadPart)*1.0/freq.QuadPart);
	return 0;
}
//10.014788  sec
赵4老师 2012-11-13
  • 打赏
  • 举报
回复
Counter*Frequency=Times
hhhhhhio110 2012-11-13
  • 打赏
  • 举报
回复
我想问这函数是怎么用的。按2楼的说法 是不是应该这样 #include <Windows.h> #include <iostream> using namespace std; int main() { LARGE_INTEGER a; LARGE_INTEGER b; QueryPerformanceFrequency(&a); QueryPerformanceCounter(&a); Sleep(1000); QueryPerformanceCounter(&b); cout<<a.HighPart<<" "<<a.LowPart<<" "<<a.QuadPart<<endl; cout<<b.HighPart<<" "<<b.LowPart<<" "<<b.QuadPart<<endl; } 输出结果为 12 574446058 52114053610 12 578007334 52117614886 代表什么意思呢
luciferisnotsatan 2012-11-13
  • 打赏
  • 举报
回复
MSDN2005版 QueryPerformanceFrequencySee Also Time Functions | QueryPerformanceCounter | Supporting High-Resolution Timers | Timer Driver Reference Requirements OS Versions: Windows CE 2.0 and later. Header: Winbase.h. Link Library: Coredll.lib. This function retrieves the frequency of the high-resolution performance counter if one is provided by the OEM. BOOL QueryPerformanceFrequency( LARGE_INTEGER* lpFrequency ); Parameters lpFrequency [out] Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, the value passed back through this pointer can be zero. Return Values If the function succeeds, it returns TRUE to indicate that a performance frequency value was successfully filled in. If the function fails, it returns FALSE. Remarks If the hardware does not support a high frequency counter, QueryPerformanceFrequency will return 1000 because the API defaults to a milliseconds GetTickCount implementation. Requirements OS Versions: Windows CE 2.0 and later. Header: Winbase.h. Link Library: Coredll.lib.
竞天问 2012-11-13
  • 打赏
  • 举报
回复
引用 7 楼 hhhhhhio110 的回复:
额 我是想计算执行一个算法所需要的时间
用GetProcessTime比较好
hhhhhhio110 2012-11-13
  • 打赏
  • 举报
回复 1
额 我是想计算执行一个算法所需要的时间
漫步者、 2012-11-12
  • 打赏
  • 举报
回复
引用 5 楼 taodm 的回复:
楼主要纳秒极的精度是要干什么呢?
你懂的
taodm 2012-11-12
  • 打赏
  • 举报
回复
楼主要纳秒极的精度是要干什么呢?
转角天边 2012-11-12
  • 打赏
  • 举报
回复
楼主你的MSDN不会是删节版吧 下面是MSDN里的描述 QueryPerformanceFrequency The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running. BOOL QueryPerformanceFrequency( LARGE_INTEGER *lpFrequency // current frequency ); Parameters lpFrequency [out] Pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware does not support a high-resolution performance counter, this parameter can be zero. Return Values If the installed hardware supports a high-resolution performance counter, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. For example, if the installed hardware does not support a high-resolution performance counter, the function fails.
armsword 2012-11-12
  • 打赏
  • 举报
回复
QueryPerformanceFrequency() 使用MSDN可以查看的到的。 LZ你再试试看。
图灵狗 2012-11-12
  • 打赏
  • 举报
回复
QueryPerformanceFrequency() - 基本介绍 类型:Win32API 原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency); 作用:返回硬件支持的高精度计数器的频率。 返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败。 QueryPerformanceFrequency() - 技术特点 供WIN9X使用的高精度定时器:QueryPerformanceFrequency()和QueryPerformanceCounter(),要求计算机从硬件上支持高精度定时器。需包含windows.h头文件。 函数的原形是: BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency); BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount); 数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下: typeef union _ LARGE_INTEGER { struct { DWORD LowPart; LONG HighPart; }; LONGLONG QuadPart; } LARGE_INTEGER; 在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间。
漫步者、 2012-11-12
  • 打赏
  • 举报
回复
QueryPerformanceFrequency
The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. 

BOOL QueryPerformanceFrequency(
  LARGE_INTEGER *lpFrequency   // address of current frequency
);
 
Parameters
lpFrequency 
Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero. 
Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the installed hardware does not support a high-resolution performance counter, the return value is zero. 

QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in winbase.h.
  Import Library: Use kernel32.lib.

See Also

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧