Windows CE、.NET - 如何获取 CPU 滴答声或以(微)毫秒为单位的当前时间?
我想测试一下我写的代码行有多难。我需要知道完成它们需要多长时间(或多少个 CPU 时钟周期)。
我在 Google 上使用 Windows CE、紧凑的 .NET Framework 2、VB.NET 2005
,在这里我找到的解决方案仅适用于桌面 Windows,而不是移动设备。我尝试了以下内容:
Private Declare Function GetTickCount Lib "kernel32" () As Long
'and
Private Declare Function GetTickCount Lib "coredll.lib" () As Long
Dim lngStart As Long = GetTickCount() 'did not work
System.Diagnostics.Stopwatch ' does not exist
System.DateTime.Now() ' resolution only seconds I need at least milliseconds
System.Environment.TickCount ' same as Now()
(DateTime.Now-new DateTime(1970,1,1)).TotalMilliseconds ' only seconds
...以及更多。 没有任何效果。
你能帮忙吗?
I would like to test how difficult are lines of code I wrote. I need to know how long (or how many CPU ticks) it takes to complete them.
I use Windows CE, compact .NET framework 2, VB.NET 2005
On Google and here I found only solutions that work on desktop Windows, not mobile. I tried following:
Private Declare Function GetTickCount Lib "kernel32" () As Long
'and
Private Declare Function GetTickCount Lib "coredll.lib" () As Long
Dim lngStart As Long = GetTickCount() 'did not work
System.Diagnostics.Stopwatch ' does not exist
System.DateTime.Now() ' resolution only seconds I need at least milliseconds
System.Environment.TickCount ' same as Now()
(DateTime.Now-new DateTime(1970,1,1)).TotalMilliseconds ' only seconds
... and much more. Nothing worked.
Can you help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GetTickCount
API 返回一个 DWORD,它是一个 32 位整数。这对应于托管Integer
而不Long
,因此这解释了P/Invoke 失败。DateTime.Now
只是调用GetSystemTime
API,在 2.0 时代以来我使用过的每台 CE 设备中,该 API 都没有毫秒分辨率。现在,任何设备的 OEM 都可以为其提供毫秒分辨率,但我从未见过它(好吧,我在为特别需要它的客户构建的操作系统中见过一次)。合理的解决方法 可以在这里找到。Environment.TickCount
调用GetTickCount
API,该 API 通常会返回自处理器启动以来的毫秒数,并在大约 22 天时进行换行。它不是处理器滴答声。它要慢得多。编辑
由于您在问题中提到了
Stopwatch
类:GetTickCount
API returns a DWORD, which is a 32-bit integer. That corresponds to a managedInteger
not aLong
so that's explains the P/Invoke failure.DateTime.Now
just calls theGetSystemTime
API which, in every CE device I've used since back in the 2.0 days, did not have millisecond resolution. Now the OEM of any device could give it ms resolution, but I've never seen it (well I saw it once in an OS that I built for a customer who specifically wanted it). A resonable workaround can be found here.Environment.TickCount
calls theGetTickCount
API, which typically returns milliseconds since the processor started, with a wrap at something like 22 days. It is not processor ticks. It is much slower.QueryPerformanceFrequency
andQueryPerformanceCounter
to get (obviously) the frequency and current value. This often has a hns (hundreds of nanoseconds) resolution. Though if you're concerned about things at this level, I might begin to question your choice of managed code. At any rate, an example for this can be found here.EDIT
Since you mentioned the
Stopwatch
class in your question: