惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
SegmentFault 最新的问题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Attack and Defense Labs
Attack and Defense Labs
F
Full Disclosure
Vercel News
Vercel News
N
News | PayPal Newsroom
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
H
Heimdal Security Blog
P
Privacy International News Feed
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
L
Lohrmann on Cybersecurity
D
Docker
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
S
Secure Thoughts
博客园 - Franky
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Latest news
Latest news
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学

博客园 - 单车骑客

安装vs2005 sp1 出现错误1718,文件未通过数字签名检查,解决方法 HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解(转载) 为新闻标题加颜色 - 单车骑客 - 博客园 GridView中如何做删除提示信息 GridView绑定多个参数值 - 单车骑客 - 博客园 Url重写 解决“不重新发送信息,则无法刷新新网页”的方案 - 单车骑客 - 博客园 powerdesigner将PDM转换到OOM时,code与name同时同步转换 Powerdesgner数据模型设计中,属性名同名问题的解决方法 正则表达式30分钟入门教程(转) asp.net页面生命周期 scrollWidth,clientWidth等区别 用js删除表中一行 存储过程中如何处理分页 SQL函数:分离字符串,并返回分离后字符的表 SQL2005如何进行分页?? SQL中,内连接,外连接的书写格式 javascript正则表达式 下拉菜单
提供一个获取高精度时间类(转)
单车骑客 · 2008-07-07 · via 博客园 - 单车骑客

此文转载于网络:http://dotnet.chinaitlab.com/ASPNET/742827.html

    如果你觉得用 DotNet 自带的 DateTime 获取的时间精度不够,解决的方法是通过调用 QueryPerformanceFrequency 和 QueryPerformanceCounter这两个API来实现。

  /// <summary>
/// 获取时间的精度
/// </summary>
/// <param name="PerformanceFrequency"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport(
"kernel32")]
static private extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
/// <summary>
/// 获取时间计数
/// </summary>
/// <param name="PerformanceCount"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport(
"kernel32")]
static private extern bool QueryPerformanceCounter(ref long PerformanceCount);

    下面是完整的封装代码:

 /// <summary>
/// 定义一个高精度的时间类
/// </summary>
    public class Timer
{
#region private members
private long ticksPerSecond = 0;
private long elapsedTime = 0;
private long baseTime = 0;
#endregion
#region windows API
/// <summary>
/// 获取时间的精度
/// </summary>
/// <param name="PerformanceFrequency"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport(
"kernel32")]
static private extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
/// <summary>
/// 获取时间计数
/// </summary>
/// <param name="PerformanceCount"></param>
/// <returns></returns>
        [SuppressUnmanagedCodeSecurity]
[DllImport(
"kernel32")]
static private extern bool QueryPerformanceCounter(ref long PerformanceCount);
#endregion
#region constructors
/// <summary>
/// new
/// </summary>
        public Timer()
{
// Use QueryPerformanceFrequency to get frequency of the timer
            if (!QueryPerformanceFrequency(ref ticksPerSecond))
throw new ApplicationException("Timer: Performance Frequency Unavailable");
Reset();
}
#endregion
#region public methods
/// <summary>
/// 重置时间相关计数器
/// </summary>
        public void Reset()
{
long time = 0;
QueryPerformanceCounter(
ref time);
baseTime
= time;
elapsedTime
= 0;
}
/// <summary>
/// 获取当前与最近一次 reset 时间差
/// </summary>
/// <returns>The time since last reset.</returns>
        public double GetTime()
{
long time = 0;
QueryPerformanceCounter(
ref time);
return (double)(time - baseTime) / (double)ticksPerSecond;
}
/// <summary>
/// 获取当前系统的时间 ticks 数
/// </summary>
/// <returns>The current time in seconds.</returns>
        public double GetAbsoluteTime()
{
long time = 0;
QueryPerformanceCounter(
ref time);
return (double)time / (double)ticksPerSecond;
}
/// <summary>
/// 获取此次与上次调用此方法的两次时间差
/// </summary>
/// <returns>The number of seconds since last call of this function.</returns>
        public double GetElapsedTime()
{
long time = 0;
QueryPerformanceCounter(
ref time);
double absoluteTime = (double)(time - elapsedTime) / (double)ticksPerSecond;
elapsedTime
= time;
return absoluteTime;
}
#endregion
}
调用代码:
Timer t
= new Timer();
t.GetAbsoluteTime();
t.GetElapsedTime();
t.Reset();
t.GetTime();