.NET Micro Framework 中 System.DateTime.Now.Ticks 的替代方案?

发布于 2024-11-17 08:28:46 字数 279 浏览 2 评论 0原文

我在一段时间关键的代码中使用 System.DateTime.Now.Ticks ,并发现它是一个瓶颈。这几乎可以肯定是由于实例化在幕后进行的 DateTime 类的开销造成的。

有人告诉我可以从 System.Environment.TickCount 获取此信息。然而,这是以毫秒为单位的,对于我所需要的测量来说太粗略了。尽管这大约快了一倍。

有没有办法获得 System.DateTime.Now.Ticks 的准确性,而无需构建 System.DateTime.Now 的开销?

I'm using System.DateTime.Now.Ticks in a time-critical piece of code, and am finding it to be a bottleneck. This is almost certainly due to the overhead of instantiating a DateTime class that goes on behind the scenes.

I was told that I could get this information from System.Environment.TickCount. However, this is in milliseconds, which are too coarse a measurement for what I need. Although this was roughly twice as fast.

Is there some way to get the accuracy of System.DateTime.Now.Ticks without the overhead of constructing System.DateTime.Now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

忆梦 2024-11-24 08:28:46

许多微控制器都有一个由 32768 Hz 晶体运行的实时时钟,该晶体为硬件中内置的纹波定时器或脉冲计数器提供信号。

例如,这个文档:
http://www.ghiElectronics.com/downloads/FEZ/Beginners %20guide%20to%20NETMF.pdf
第 91 页介绍了在 USBIZI 设备上使用脉冲计数器。

它还链接到该文档
http://www.keil.com/dd/docs/datashts/philips /lpc23xx_um.pdf
哪一页上。第585章
6.2.2 时钟节拍计数器寄存器 (CTCR - 0xE002 4004)

因此,您应该能够读取该寄存器以获得 65536 个节拍的滚动计数。如果两次读取该寄存器之间的经过时间小于 0.5 秒,则进行带符号减法,如果小于 0,则加上 65536 将得到经过的时间。

Many microcontrollers have a Real Time Clock which runs of a 32768 Hz crystal, which feeds a ripple timer or a pulse counter built into the hardware.

For instance, this document:
http://www.ghielectronics.com/downloads/FEZ/Beginners%20guide%20to%20NETMF.pdf
on page 91 describes using a pulse counter on the USBIZI device.

It also links to this document
http://www.keil.com/dd/docs/datashts/philips/lpc23xx_um.pdf
which on p. 585 describes the TickCounterRegister
6.2.2 Clock Tick Counter Register (CTCR - 0xE002 4004)

So, you should be able to read that register to get a rolling count of 65536 ticks. If your elapsed time is less than a .5 seconds between two reads of that register, a signed subtraction, and adding 65536 if less than 0 will give you the elapsed time.

·深蓝 2024-11-24 08:28:46

Eric,

正如您所理解的,当您调用 DateTime 的 Ticks 属性时,发生的情况是 .NET 框架将日期和时间(以毫秒为单位)转换为 Tick 值。换句话说,您返回的 Tick 值不是对经过的时间的测量,而只是日期时间结构的转换,日期时间结构本身以毫秒为单位。您可能会觉得通过使用 Ticks 获得了一些高分辨率的计时,但实际上,毫秒与使用 DateTime 获得的一样好。刻度值将经过的毫秒数除以 0x2500 之类的值,这就是您获得高精度数字的原因。但请记住,时间仍然以毫秒为单位流逝。

Eric,

Just so you understand too, when you call the Ticks property of DateTime, what is happening is the .NET framework is converting the date and time (in milliseconds) to a Tick value. In other words, the Tick value you are getting back is not a measurement of time elapsed, but just a conversion of the date time structure, which itself is measured in milliseconds. You may feel like you are getting some high resolution timing by using Ticks, but in reality, milliseconds is as good as you are getting using DateTime anyway. The tick value divides the number of milliseconds elapsed by something like 0x2500 and that is why you are getting a high precision number. But keep in mind that time is still elapsing in milliseconds.

合久必婚 2024-11-24 08:28:46

答案取决于您需要时间做什么。例如,如果您尝试比较应用程序同一运行实例中的两个时间实例,则可以尝试使用 DateTime.UtcNow,它不会针对区域设置或 DST 进行调整。

如果您需要在特定时间触发事件,请使用计时器(如果您非常关心粒度,则可能使用 System.Threading.Timer,尽管 System.Timers.Timer 更易于使用)。

如果您因其他原因需要它,请注明。

The answer depends on what you need the time for. For example, if you are trying to compare two time instances from the same running instance of your application, then you can try using DateTime.UtcNow, which is not adjusted for locale or DST.

If you need to trigger an event at a specific time, use a Timer (probably System.Threading.Timer if you care that much about the granularity, although System.Timers.Timer is easier to use).

If you need it for some other reason, please specify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文