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

推荐订阅源

H
Help Net Security
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
I
Intezer
Y
Y Combinator Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Stack Overflow Blog
Stack Overflow Blog
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
云风的 BLOG
云风的 BLOG
博客园_首页
V2EX - 技术
V2EX - 技术
T
Threat Research - Cisco Blogs
腾讯CDC
宝玉的分享
宝玉的分享
博客园 - 叶小钗
罗磊的独立博客
S
Securelist
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
博客园 - 司徒正美
W
WeLiveSecurity
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
NISL@THU
NISL@THU
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
IT之家
IT之家

博客园 - 代码泪

VC2008下配置boost库使用正则表达式[转] 解决下载/上传大文件问题(IIS6)[转] 动态创建DataTable[转] C#格式化字符串,日期,时间,货币[转] Winform数据库连接app.config文件配置 C# 连接SQL数据库 常用连接字符串 sql得到当前系统时间得 日期部分 update select [SqlException (0x80131904): 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。……(provider: TCP 提供程序, error: 0 - 由于目标机器积极拒绝,无法连接。)] VS 网站发布失败 [收藏]"Automation服务器不能创建对象" 的多种解决办法 Hashtable, ArrayList, List, Dictionary学习[转] VSTS for Testers学习笔记目录[转] SQL Server 2005安全配置[转] 超时时间已到.错误及Max Pool Size设置 execute sp_executesql 用变量获取返回值【转】 使用存储过程并返回值与及返回值的获得方法[转] js取得dropdownlist的text,value[转] - 代码泪 - 博客园 input text 的事件及方法
DataTime.Now.Ticks精确的时间单位[转]
代码泪 · 2011-06-09 · via 博客园 - 代码泪

DataTime.Now.Ticks

getTime
public long getTime()

返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

返回: 自 1970 年 1 月 1 日 00:00:00 GMT 以来此日期表示的毫秒数。

.net DateTime.Ticks

public long Ticks {get;}
   
 属性值

表示此实例的日期和时间的刻度数。该值介于 MinValueMaxValue 之间。

备注

此属性的值为自 0001 年 1 月 1 日午夜 12:00 以来所经过时间以 100 毫微秒为间隔表示时的数字。

好了一个返回的是毫秒一个返回的是微秒,所以知道毫秒与微妙之间的转化也是有必要的

1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)
1秒=1,000,000,000 纳秒(ns)
1纳秒=1/1,000,000,000秒(s)
1秒=1,000,000,000,000 皮秒(ps)

1 毫秒 = 10^-3 秒, ------->10的-3次方 小数点从1开始向左移3位即0.001
1 微秒 = 10^-6 秒,
1 毫微秒 = 10^-9 秒,
100 毫微秒 = 10^-7 秒。

Console.WriteLine(DateTime.Now.Ticks); // 输出:633603924670937500

也就是说,从0001 年 1 月 1 日午夜 12:00:00 以来到现在已经过了  633603924670937500 * 10^-7 秒。

很少用,除非需要很精确地知道从那时(1年1月1日)开始过了多少时间。

比如精确地计算两个时间差时(想知道某段程序运行了多少毫微秒)就可以用到。

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. class Sentence  
  5. {  
  6.   static void Main()  
  7.   {  
  8.     long ticks0 = DateTime.Now.Ticks;  
  9.     for (int i = 0; i < int.MaxValue; i++)  
  10.     {  
  11.       // ...  
  12.     }  
  13.     long ticks1 = DateTime.Now.Ticks;  
  14.     long n = (ticks1 - ticks0) * 100;  
  15.     Console.WriteLine("上面这段程序运行了{0}毫微秒", n);  
  16.   }  
  17. }  

转换成比用毫微秒更直观些:

  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. class Sentence  
  5. {  
  6.   static void Main()  
  7.   {  
  8.     long ticks0 = DateTime.Now.Ticks;  
  9.     for (int i = 0; i < int.MaxValue; i++)  
  10.     {  
  11.       // ...  
  12.     }  
  13.     long ticks1 = DateTime.Now.Ticks;  
  14.     double n = (ticks1 - ticks0) / 10000000.0;  
  15.     Console.WriteLine("上面这段程序运行了{0}秒", n);  
  16.   }  
  17. }  

获取高精度的时间差,可以用来分析页面运行时间的长短

DateTime.Now的精度是很低,这个低的意思是,两次获取的DateTime.Now的Ticks的差,只是一个较大数的整数倍。例如在我 的机器上,这个差最小是10.114ms。所以,如果我用DateTime.Now来计算时间差,那么就无法精确到10ms以内。

后来发现ASP.NET的TRACE的精度很高,用Reflector看它的实现,发现了它是使用这两个方法的:

参考MSDN:How To: Time Managed Code Using QueryPerformanceCounter and QueryPerformanceFrequency

我自己了按照这个写了个类,代码如下:

  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. public class A  
  4. {  
  5.     [DllImport("kernel32.dll")]  
  6.     static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount);  
  7.     [DllImport("kernel32.dll")]  
  8.     static extern bool QueryPerformanceFrequency([In, Out] ref long lpFrequency);  
  9.   
  10.     static long _f = 0;  
  11.   
  12.     static public long GetTickCount()  
  13.     {  
  14.         long f = _f;  
  15.   
  16.         if (f == 0)  
  17.         {  
  18.             if (QueryPerformanceFrequency(ref f))  
  19.             {  
  20.                 _f = f;  
  21.             }  
  22.             else  
  23.             {  
  24.                 _f = -1;  
  25.             }  
  26.         }  
  27.         if (f == -1)  
  28.         {  
  29.             return Environment.TickCount * 10000;  
  30.         }  
  31.         long c = 0;  
  32.         QueryPerformanceCounter(ref c);  
  33.         return (long)(((double)c) * 1000 * 10000 / ((double)f));  
  34.     }  
  35.   
  36.     //GetTickCount()为0时的DateTime.Ticks值  
  37.     static long _tc = 0;  
  38.   
  39.     //这个返回的不是真正的精确时间,但时间与时间的差是精确的。  
  40.     //GetExactNow与DateTime.Now的偏差比DateTime.Now的精度还要小,所以该偏差  
  41.     static public DateTime GetExactNow()  
  42.     {  
  43.         if (_tc == 0)  
  44.         {  
  45.             long tc = GetTickCount();  
  46.             DateTime dt = DateTime.Now;  
  47.             _tc = dt.Ticks - tc;  
  48.             return dt;  
  49.         }  
  50.   
  51.         return new DateTime(_tc + GetTickCount());  
  52.     }  
  53. }  

在ASP。NET的应用,可以在Global.asax的Application_BeginRequest事件中加入代码来纪录程序开始时的TickCount:

Context.Items["BeginRequestTickCount"]=A.GetTickCount();

然后在页面输出的后面:

<html>....
<div align="center">
<%=new TimeSpan(A.GetTickCount()-(long)Context.Items["BeginRequestTickCount"]).TotalMilliseconds%>
</div>
</body></html>

这样就可以达到获取页面运行时间值了。(当然输出TotalMilliseconds后Asp.Net还要一些后期工作的,不过这个时间应该只需要0.n ms)

1秒=1000000000毫微秒   10亿

1秒=1000000000毫微秒

1秒=1000000000毫微秒(10亿分之一秒)

1秒=1000豪秒
1毫秒=1000微秒
1微秒=1000毫微秒
所以1秒=1000*1000*1000=1000000000毫微秒

1秒=1000豪秒
1毫秒=1000微秒
1微秒=1000毫微秒
1毫微秒=1纳秒
1纳秒=10埃秒

1秒   =   1000   毫秒
        =   1000000   微秒
        =   1000000000   毫微秒
        =   1000000000   纳秒
        =   1000000000000   皮秒

1s   =   1000   ms
      =   1000000   us
      =   1000000000   ns
      =   1000000000000   ps

1(秒)=10的9次方(毫微秒)

<!--End_rbody_41637559//-->

<!--End_rbody_41637167//-->