In some applications exact time measurement methods are very important.

一些应用程序中精确的时间测量是非常重要的。

The often used Windows API method GetTickCount() retrieves the number of milliseconds that have elapsed since the system was started, but the GetTickCount() function only archieve resolutions of 1ms and on the other side they are very imprecise.

常使用WindowApi方法GetTickCount()提取系统启动后使用的毫秒,但是GetTickCount()方法只检索1ms的精度,另一方面他也是非常不精确的

So, for exact time measurement we should find another method.

因此,为了提取精确的时间,我们需要寻找其他的方法。

High resolution timing is supported in Win32 by the QueryPerformanceCounter() and QueryPerformanceFrequency() API methods.

Win32通过QueryPerformanceCounter()和QueryPerformanceFrequency() API支持高精度的时间

This timer functions has much better resolution than the "standard" millisecond-based timer calls, like the GetTickCount() method.

这个时间函数比标准的毫秒级要精确的多

On the other side there is also a little bit overhead when calling this "unmanaged" API methods from C#, but it's better than using the very imprecise GetTickCount() API function.

也有一些开销用非托管的API方法,但是比使用不精确的GetTickCount() API好的多。

The first call, QueryPerformanceCounter(), queries the actual value of the high-resolution performance counter at any point.

The second function, QueryPerformanceFrequency(), will return the number of counts per second that the high-resolution counter performs.

To retrieve the elapsed time of a code section you have to get the actual value of the high-resolution performance counter immediately before and immediately after the section of code to be timed.

The difference of these values would indicate the counts that elapsed while the code executed.

这些值得变化可以指示代码执行花费的时间。

The elapsed time can be computed then, by dividing this difference by the number of counts per second that the high-resolution counter performs (the frequency of the high-resolution timer).

花费时间就可以计算,通过除以高精度时间的频率

duration = (stop - start) / frequency

For more information about QueryPerformanceCounter and QueryPerformanceFrequency read the documentation on MSDN.

http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C

WW中的类:

 1 using System;
 2 using System.Runtime.InteropServices;
 3
 4 namespace WorldWind
 5 {
 6     public sealed class PerformanceTimer
 7     {
 8         #region Instance Data
 9
10         public static long TicksPerSecond;
11         #endregion
12
13         #region Creation
14
15         /// <summary>
16         /// Static class
17         /// </summary>
18          private PerformanceTimer()
19         {
20         }
21
22         /// <summary>
23         /// Static constructor
24         /// </summary>
25         static PerformanceTimer()
26         {
27             // Read timer frequency
28             long tickFrequency = 0;
29             if (!QueryPerformanceFrequency(ref tickFrequency))
30                 throw new NotSupportedException("The machine doesn't appear to support high resolution timer.");
31             TicksPerSecond = tickFrequency;
32
33             System.Diagnostics.Debug.WriteLine("tickFrequency = " + tickFrequency);
34         }
35         #endregion
36
37         #region High Resolution Timer functions
38
39         [System.Security.SuppressUnmanagedCodeSecurity]
40         [DllImport("kernel32")]
41         private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
42
43         [System.Security.SuppressUnmanagedCodeSecurity]
44         [DllImport("kernel32")]
45         public static extern bool QueryPerformanceCounter(ref long PerformanceCount);
46
47         #endregion
48     }
49 }

备注:C#可以采用下面方案实现 :

Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间。

Stopwatch 类为托管代码内与计时有关的性能计数器的操作提供帮助。 具体说来, Frequency 字段和 GetTimestamp 方法可以用于替换非托管 Win32 APIQueryPerformanceFrequency 和 QueryPerformanceCounter。

[WorldWind学习]18.High-Performance Timer in C#相关推荐

  1. DSP学习(5)—— Timer的使用

    DSP学习(5)-- Timer的使用 文章目录 DSP学习(5)-- Timer的使用 前言 一.创建Timer 1. 法一:图形界面创建 2. 法二:代码方式创建 二.相关问题 前言 本文记录学习 ...

  2. WorldWind学习系列十五:如何切割影像和DEM数据及其在WW中的应用配置

    原文转自:http://www.cnblogs.com/wuhenke/archive/2010/01/03/1638499.html WorldWind学习系列十四中我从代码上分析如何加载DEM数据 ...

  3. 系统管理Lesson 18.Managing Performance

    系统管理Lesson 18.Managing Performance 1.Oracle数据库的性能优化过程一般包含哪几种活动? 18-5 2.性能规划阶段主要涉及到哪些选项? 18-6 3.实例优化过 ...

  4. WorldWind学习系列一:顺利起航篇

    今天从官方下载WorldWind 1.4版的源代码开始研究,可是运行时,发现有很多问题.下面是我的解决对策,帮助遇到相同问题的网友顺利起航! 错误1:      原因:无法找到引用的DLL文件 解决方 ...

  5. [WorldWind学习]17.视域调度(视域体裁剪)

    视域调度(视域体裁剪) 在WW中用户改变自己的的视角,纹理影像和高程会动态加载,在视野范围内的影像和DEM显示,超出视域范围的瓦片则不显示.不仅是瓦片,太阳.大气网格.三维模型ModelFeature ...

  6. [WorldWind学习]12.WavingFlags和WavingFlagLayer

    WW目前的服务器似乎都连不上了,不知道Java版的是不是可以! WW实现了旗帜标注,鼠标移动到旗帜的位置,旗帜会高亮显示.点击,探出对话框显示标注的信息. 1.WavingFlagLayer对象 pu ...

  7. WorldWind学习系列十一:Virtual Earth插件学习

    学习WorldWind有很长时间了,理论学习算是基本完成了.我体会是WW的学习主要分为两大步:WW框架体系学习和WW插件学习.学习WW插件逐步深入后,必然要首先学习Direct3D编程,这也算是我的经 ...

  8. WorldWind学习系列七:Load/Unload Plugins——投石问路篇

    原文转自:http://www.cnblogs.com/wuhenke/archive/2009/12/15/1625102.html 今天原计划把Load/Unload Plugins完全弄明白,可 ...

  9. [WorldWind学习]5.相机对象

    首先查看WorldWindow的事件:OnMouseUp.OnMouseMove.HandleKeyDown,这几个方法中多次调用this.drawArgs.WorldCamera的各种属性实现了场景 ...

最新文章

  1. 不丹的启示:用国民幸福总值替代GDP
  2. variables needed for gradient computation has been modified by an inplace operation
  3. union和union all有什么区别
  4. 计算机windows多用户,windows Server 2012 专业版配置多用户远程桌面连接
  5. 预览速度提升30倍,这是什么黑科技?(天猫618之3D渲染篇)
  6. 【Nginx】通过反向代理配置本地图床功能
  7. cross validation交叉验证
  8. 深度学习——ReLU在x=0的时候是条线,为什么什么是非线性函数?
  9. SQL SERVER占用CPU过高排查和优化
  10. [Spring实战系列](8)Spring注入方式之setter注入
  11. Unity3D基础31:脚本生命周期
  12. 网页打开摄像头_只要5分钟,快速掌握摄像头课件直播技巧
  13. 12款网盘搜索神器以备不时之需要
  14. mysql yum 安装
  15. Matlab textscan
  16. python中histogram_python – 了解Pillow中的histogram()
  17. vue-amap 根据地址 查询经纬度
  18. Latex添加中文支持和A4纸张设置
  19. 信奥中的数学:加法原理和乘法原理
  20. Word文档怎么进行加密

热门文章

  1. 如何判断离散数组 是递增趋势_期货交易中,如何通过交易周期判断趋势,做到顺势而为?...
  2. html border阴影效果_一篇文章教会你使用html+css3制作炫酷效果
  3. inner join 和 exists 效率_一阵骚操作,我把SQL执行效率提高了10000000倍!
  4. python顺序结构实验_Python程序设计实验报告二:顺序结构程序设计
  5. 光流 | 基于光流法实现视觉里程计Visual Odometry(源代码)
  6. 使用Samba实现文件共享
  7. mysql的查询语句怎么优化_MySQL查询语句如何优化
  8. c 和php 加密,加载由blenc加密的页面时出错(C和PHP代码)
  9. Numpy 排序 -- sort()、argsort()
  10. PyQt5 笔记3 -- 信号与槽