Windows CE、.NET - 如何获取 CPU 滴答声或以(微)毫秒为单位的当前时间?

发布于 2024-11-16 23:46:54 字数 689 浏览 2 评论 0原文

我想测试一下我写的代码行有多难。我需要知道完成它们需要多长时间(或多少个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-11-23 23:46:54
  1. GetTickCount API 返回一个 DWORD,它是一个 32 位整数。这对应于托管Integer而不Long,因此这解释了P/Invoke 失败。
  2. DateTime.Now 只是调用 GetSystemTime API,在 2.0 时代以来我使用过的每台 CE 设备中,该 API 都没有毫秒分辨率。现在,任何设备的 OEM 都可以为其提供毫秒分辨率,但我从未见过它(好吧,我在为特别需要它的客户构建的操作系统中见过一次)。合理的解决方法 可以在这里找到
  3. Environment.TickCount 调用 GetTickCount API,该 API 通常会返回自处理器启动以来的毫秒数,并在大约 22 天时进行换行。它不是处理器滴答声。它要慢得多。
  4. 某些设备支持更高分辨率的计数器,可以通过 P/Invoking QueryPerformanceFrequency 和 QueryPerformanceCounter 查询该计数器以获取(显然)频率和当前值。这通常具有 hns(数百纳秒)分辨率。不过,如果您担心这个级别的事情,我可能会开始质疑您对托管代码的选择。无论如何,可以在此处找到此示例
  5. 如果您正在查看代码性能和 CF,则这篇文章虽然有点旧,但仍然有效且内容丰富。

编辑

由于您在问题中提到了 Stopwatch 类:

  1. Stopwatch 类的实现可与旧版本中的 CF 1.0 和 CF 2.0 配合使用1.x SDF 源代码。您可以在此处下载完整(免费)1.4 源代码(页面底部中间) )。
  1. The GetTickCount API returns a DWORD, which is a 32-bit integer. That corresponds to a managed Integer not a Long so that's explains the P/Invoke failure.
  2. DateTime.Now just calls the GetSystemTime 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.
  3. Environment.TickCount calls the GetTickCount 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.
  4. Some devices support a higher-resolution counter, which can be queried by P/Invoking QueryPerformanceFrequency and QueryPerformanceCounter 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.
  5. If you're looking at code performance and the CF then this article, while a bit old, is still valid and informative.

EDIT

Since you mentioned the Stopwatch class in your question:

  1. There's an implementation of the Stopwatch class that works with CF 1.0 and CF 2.0 in the old 1.x SDF source code. You can download the full (free) 1.4 source here (bottom middle of the page).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文