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

hhhhhhio110 2012-11-12 06:41:52
如题,网上搜了一下,用QueryPerformanceFrequency()这个函数,但是我用VS确找不到这个函数,用MSDN也查不到,这是为什么,有没有其他的一些方法呢
...全文
1295 14 打赏 收藏 转发到动态 举报
写回复
用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
Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以直接在你的网页里面显示搜查的结果。 github-java-api github-java-api 是 Github 网站 API 的 Java 语言版本。 java缓存工具 SimpleCache SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的定义 4. 使用redis作缓存时,支持list类型的高数据结构,更适合论坛帖子列表这种类型的数据 5. 支持混合使用redis缓存和memcached缓存。可以将列表数据缓存到redis中,其他kv结构数据继续缓存到memcached 6. 支持redis的主从集群,可以做读写分离。缓存读取自redis的slave节点,写入到redis的master节点。 Java对象的SQL接口 JoSQL JoSQL(SQLforJavaObjects)为Java开发者提供运用SQL语句来操作Java对象集的能力.利用JoSQL可以像操作数据库中的数据一样对任何Java对象集进行查询,排序,分组。 搜索自动提示 Autotips AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用程序)。 重复文件检查工具 FindDup.tar FindDup 是一个简单易用的工具,用来检查计算机上重复的文件。 OpenID的Java客户端 JOpenID JOpenID是一个轻量的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor 的文件持久化组件,以及一个高吞吐量的可靠事务日志组件。 Google地图JSP标签库 利用Google:maps JSP标签库就能够在你的Web站点上实现GoogleMaps的所有功能而且不需要javascript或AJAX编程。它还能够与JSTL相结合生成数据库驱动的动态Maps。 OAuth 实现框架 Agorava Agorava 是一个实现了 OAuth 1.0a 和 OAuth 2.0 的框架,提供了简单的方式通过社交媒体进行身份认证的功能。 Eclipse的JavaScript插件 JSEditor JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K

64,639

社区成员

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

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