测量时间(以微秒为单位) [英] Measuring time in microseconds

查看:119
本文介绍了测量时间(以微秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个函数,该函数在C#或C ++中以毫秒为单位测量时间.我如何使用Visual Studio 2008做到这一点?

I need to create a function that measures time in microseconds in C# or C++. How can I do that using Visual Studio 2008?

推荐答案

在使用C ++的嵌入式系统中,这通常很容易做到.但是,在Windows中则不是. :(

In embedded systems using C++, that''s often easily done. In Windows, however, it''s not. :(

您可以尝试以下操作,但是请记住,最接近的时间约为15到20毫秒(+/-几毫秒,具体取决于您的硬件):

You might try the following, but remember that the closest you''d get to would be something like 15 to 20 milliseconds (+/- a few milliseconds, depending on your hardware):

  • QueryPerformanceCounter():此函数将返回非常精确的时间计数,但是由于Windows不是实时操作系统,因此有时该计数没有意义.请注意,在某些情况下,计数甚至可能会倒退!
  • 多媒体计时器功能:我从没使用过这些功能,只听说过它们.我知道它们的工作方式比QueryPerformanceCounter()可靠一些,但是它们也有局限性.
  • QueryPerformanceCounter(): This function will return a time count that''s very precise, but because Windows isn''t a real-time operating system, sometimes the counts won''t make sense. I understand that the count can even seem to go backwards in some situations!
  • Multimedia Timer functions: I''ve never used these, only heard about them. I understand that they work somewhat more reliably than QueryPerformanceCounter(), but they too have limitations.


如果要说的话,只需添加如果测量的时间只有微秒,恐怕Windows并非适合您的操作系统.只是不够准确.

Just to add, if you mean how to measure a SMALL number of microseconds, I''m afraid that Windows is not the OS for you.  It''s just not that accurate.


写道:​​

C#或C ++.如何使用Visual Studio 2008做到这一点?

C# or C++. How can I do that using Visual Studio 2008?



Visual Studio的版本(或任何其他IDE)或尝试使用此语言的编程语言均无关紧要.

我之所以这样说,是因为Windows甚至无法提供毫秒的精度.微秒是毫秒精度的千倍倍,因此您需要放弃在Windows上尝试此操作.

而是选择一个RTOS.



The version of visual studio (or any other IDE), or the programming language that you may use to attempt this is irrelevant.

I say it because Windows can''t even give you the precision of milliseconds. Microseconds is a thousand times the precision of milliseconds, so you need to give up trying this on Windows.

Choose an RTOS instead.


这篇关于测量时间(以微秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆