[WorldWind学习]18.High-Performance Timer in C#
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中的类:
using System;
using System.Runtime.InteropServices; namespace WorldWind
{
public sealed class PerformanceTimer
{
#region Instance Data public static long TicksPerSecond;
#endregion #region Creation /// <summary>
/// Static class
/// </summary>
private PerformanceTimer()
{
} /// <summary>
/// Static constructor
/// </summary>
static PerformanceTimer()
{
// Read timer frequency
long tickFrequency = ;
if (!QueryPerformanceFrequency(ref tickFrequency))
throw new NotSupportedException("The machine doesn't appear to support high resolution timer.");
TicksPerSecond = tickFrequency; System.Diagnostics.Debug.WriteLine("tickFrequency = " + tickFrequency);
}
#endregion #region High Resolution Timer functions [System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); [System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
public static extern bool QueryPerformanceCounter(ref long PerformanceCount); #endregion
}
}
备注:C#可以采用下面方案实现 :
Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间。
Stopwatch 类为托管代码内与计时有关的性能计数器的操作提供帮助。 具体说来, Frequency 字段和 GetTimestamp 方法可以用于替换非托管 Win32 APIQueryPerformanceFrequency 和 QueryPerformanceCounter。
[WorldWind学习]18.High-Performance Timer in C#的更多相关文章
- 【JS】学习18天Jquery Moblie的总结笔记。
现在是2013年11月18日1:53分,从1号开始学习JqueryMoblie(简称JQM),这些天遇到的问题,和走的弯路还真不少. 先做个小总结,做了那么多天的一些收获: ●JQM是一个手机网站/轻 ...
- ExtJS4.2学习(18)时间控件(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-22/190.html 感谢“束洋洋 ”的付出. 前言 ...
- 定时任务调度工作(学习记录 三)timer其他重要函数
TimerTask的两个重要函数: 1.cancel() 作用: 取消当前TimerTask里的任务 演示: 先在继承了TimerTask的类中添加一个计时器,然后在run方法中合适的位置添加canc ...
- 定时任务调度工作(学习记录 二)timer定时函数的用法
schedule的四种用法: 1.schedule(task,time) 参数: task----所安排的任务 time----执行任务的时间 作用: 在时间等于或超过time的时候执行且仅执行一次t ...
- go语言time包的学习(Time,Location,Duration,Timer,Ticker)
package main; import ( "time" "fmt" ) func main() { //time.Time代 ...
- swift 学习- 18 -- 自动引用计数
// Swift 使用 自动引用计数 (ARC) 机制来跟踪和管理你的应用程序的内存, 通常情况下, Swift 内存管理机制会一直起作用, 你无须自己来考虑内存的管理, ARC 会在类的实例不再被使 ...
- OpenCV学习(18) 细化算法(6)
本章我们在学习一下基于索引表的细化算法. 假设要处理的图像为二值图,前景值为1,背景值为0. 索引表细化算法使用下面的8邻域表示法: 一个像素的8邻域,我们可以用8位二进制表示,比如下面的8邻域,表示 ...
- [WorldWind学习]19.WebDownload
using System; using System.Diagnostics; using System.Globalization; using System.Net; using System.I ...
- (C/C++学习)18.C语言双向链表
说明:数组提供了连续内存空间的访问和使用,而链表是对内存零碎空间的有效组织和使用.链表又分为单向链表和双向链表,单向链表仅提供了链表的单方向访问,相比之下,双向链表则显得十分方便. 一.单向链表的节点 ...
随机推荐
- Buff系统的实现
BUFF是很多游戏都在采用的一种临时增益机制.本文讲述如何在基于关系型数据库的网页游戏中实现这一系统:如何扩展该系统:以及如何提高该系统的性能. 引言 BUFF是很多游戏都在采用的一种临时增益机制:与 ...
- Vertex and FragmentShader顶点与片段着色器
一.顶点与片段着色器简介 Vertex and FragmentShader:最强大的Shader类型,也是本系列的重点,下文中简称V&FShader,属于可编程渲染管线.使用的是CG/HLS ...
- Java web url 规范
设计URI应该遵循的原则 URI是网站UI的一部分,因此,可用的网站应该满足这些URL要求 简单,好记的域名 简短(short)的URI 容易录入的URI URI能反应站点的结构 URI是可以被用户猜 ...
- mybatis由浅入深day02_7查询缓存_7.2一级缓存_一级缓存应用
7 查询缓存 7.1 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时 ...
- Spring学习笔记--在SpEL中筛选集合
要用到Spring的util(包括util:list等),xml文件中的beans中需要添加一些有关util的信息: <?xml version="1.0" encoding ...
- linux文件锁flock【转】
转自: https://www.cnblogs.com/kex1n/p/7100107.html linux文件锁flock 在多个进程同时操作同一份文件的过程中,很容易导致文件中的数据混乱,需要 ...
- JavaScript实现本地图片上传预览功能(兼容IE、chrome、FF)
需要解决的问题有:本地图片如何在上传前预览.编辑:最近发现这个功能很多是基于flash实现的,很多JavaScript实现的代码兼容性都很差,特别是在IE和firefox和chrome三个浏览器上不兼 ...
- synchronized同步语句块
用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待比较长时间.在这样的情况下可以使用synchronized同步语句块来解 ...
- LeetCode——Valid Anagram
Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...
- 图片文字混排的垂直居中、inline-block块元素和行内元素混排的垂直居中问题
图片.文字混排: 不管图片和文字的前后位置,都要给 图片 设置 vertical-algin,而不是谁在前面给谁设置. 此方法兼容IE7+ 和其它主流浏览器.IE7-没有测. inline-block ...