wangchao.org
注册 | 登录 | 添加收藏 | 订阅该频道
 
商城汽车珠宝健康家饰女性王朝网络游戏互联网探索下载娱乐学院
 
数码 | 旅游 | 美容 | 母婴 | 家电 | 美食 | 景区 | 养生 | 手机 | 购车 | 首饰 | 美妆 | 装修 | 厨房 | 科普 | 动物 | 植物 |  | 百态 | 编程 | 商品 | 财经 | 信息 | 军事
  
 
当前位置: 王朝网络 >> c# >> 在C#中实现高性能计时
 

在C#中实现高性能计时

字体: ||
  For performance test, it is very important to measure code execution time. Without measurement, there is no way to tell if we meet performance goal.
  System.Environment.TickCount is not suitable for high resolution timing. Its resolution cannot be less than 500 milliseconds.
  System.Datetime.Now returns the current time of type DateTime. With start datetime and end datetime, we can get the interval as a value of TimeSpan by (end - start ) . TimeSpan.TotalMilliseconds or TimeSpan.Ticks may be used to read interval. From MSDN, the resolution of System.Datetime.Now depends on the system timer.
  System Approximate Resolution
  Windows NT 3.5 and later 10 milliseconds
  Windows 98 55 milliseconds
  So it is better but not high resolution at all.
  In .NET framework v1 and v1.1, we have to use P/Invoke to get high resolution reading. The class below is commonly used in performance test measurement. It is querying hardware to get high resolution performance counter. For more information (including what happens if the hardware does not support high resolution performance counter) please check MSDN for QueryPerformanceCounter and QueryPerformanceFrequency.
  public class HighResolutionTimer
  {
   private long start;
   private long stop;
   private long frequency;
   public HighResolutionTimer()
   {
   QueryPerformanceFrequency (ref frequency);
   }
   public void Start ()
   {
   QueryPerformanceCounter (ref start);
   }
   public void Stop ()
   {
   QueryPerformanceCounter (ref stop);
   }
   public float ElapsedTime
   {
   get{
   float elapsed = (((float)(stop - start)) / ((float) frequency));
   return elapsed;
   }
   }
   [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
   private static extern bool QueryPerformanceCounter( [In, Out] ref long performanceCount);
   [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
   private static extern bool QueryPerformanceFrequency( [In, Out] ref long frequency);
  }
  To illustrate the use of this class, check the code below.
   HighResolutionTimer timer = new HighResolutionTimer();
   timer.Start();
   //Perf Test
   timer.Stop();
   Console.WriteLine(timer.ElapsedTime);
  (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)
  作者:孙展波
  来源:
http://blogs.gotdotnet.com/ZHANBOS(王朝网络 wangchao.net.cn)
 
标签: C#  实现  计时  高性能  
For performance test, it is very important to measure code execution time. Without measurement, there is no way to tell if we meet performance goal. System.Environment.TickCount is not suitable for high resolution timing. Its resolution cannot be less than 500 milliseconds. System.Datetime.Now returns the current time of type DateTime. With start datetime and end datetime, we can get the interval as a value of TimeSpan by (end - start ) . TimeSpan.TotalMilliseconds or TimeSpan.Ticks may be used to read interval. From MSDN, the resolution of System.Datetime.Now depends on the system timer. System Approximate Resolution Windows NT 3.5 and later 10 milliseconds Windows 98 55 milliseconds So it is better but not high resolution at all. In .NET framework v1 and v1.1, we have to use P/Invoke to get high resolution reading. The class below is commonly used in performance test measurement. It is querying hardware to get high resolution performance counter. For more information (including what happens if the hardware does not support high resolution performance counter) please check MSDN for QueryPerformanceCounter and QueryPerformanceFrequency. public class HighResolutionTimer { private long start; private long stop; private long frequency; public HighResolutionTimer() { QueryPerformanceFrequency (ref frequency); } public void Start () { QueryPerformanceCounter (ref start); } public void Stop () { QueryPerformanceCounter (ref stop); } public float ElapsedTime { get{ float elapsed = (((float)(stop - start)) / ((float) frequency)); return elapsed; } } [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceCounter( [In, Out] ref long performanceCount); [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] private static extern bool QueryPerformanceFrequency( [In, Out] ref long frequency); } To illustrate the use of this class, check the code below. HighResolutionTimer timer = new HighResolutionTimer(); timer.Start(); //Perf Test timer.Stop(); Console.WriteLine(timer.ElapsedTime); (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at [url=http://www.microsoft.com/info/cpyright.htm]http://www.microsoft.com/info/cpyright.htm[/url]) 作者:孙展波 来源: [url=http://blogs.gotdotnet.com/ZHANBOS/commentview.aspx/fe108056-bf72-4029-accf-22ccb9e7bb7d]http://blogs.gotdotnet.com/ZHANBOS[/url]
 
声明:王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,文章内容仅供参考。
 
网友评论 查看所有评论
 
 
验证码:  
2006-01-09 00:57:18 繁體版 编辑
 
 
转载本文
UBB代码HTML代码
复制到剪贴板...
 
 最新文章
 ·印度暂缓封锁黑莓 紧盯网密服务密钥- ·黑客借钓鱼网站设陷阱-安全资讯 ·黄海波女友周诗雅性感写真-美女明星 ·一起来看看那些天雷滚滚的广告-搞笑
 ·微软推便携式触摸式鼠标Arc Tou ·图说那些世界上最美丽的地方-风景壁纸 ·快递“先签字后验货”被指违法-业内资 ·QQ.CN成为黑龙江联通IDC业务新
 ·拒绝50万美元奖金 跳槽Facebo ·网络管理员工作错误处理常见十宗罪-安 ·李彦宏百度大会 门票遭黄牛热炒-业内 ·李彦宏坦克大战 搜索可在线玩游戏-业
 ·步步追踪 破译远程控制失效之谜-应用 ·2010年度上半年全国病毒传播趋势  ·网络知识:OSPF路由协议基础-应用 ·林志颖娇妻陈若仪照片大曝光-美女明星
 ·十分钟完全体验Maxthon3.0- ·《庄园物语》8月风靡金山游戏世界 - ·表现满意 快车3.7新版下载能力测试 ·学生开学换手机 专家提醒先安全扫描-
 ·卡巴安全部队震撼发布保护网银安全-业 ·360安全:600万网民电脑龟速开机 ·卡巴斯基全力打造交易安全软件-安全资 ·搜狗“五级加速”引领高速浏览时代-网
 
 
© 2005- 王朝网络 版权所有