Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

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

Posted
Updated 26-Nov-09 11:00am
v2

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

This is because those embedded systems are designed to run on very selected hardware and therefore the operating system has a deep knowledge of the behaviour of the hardware, including the latencies . This enables the Operating system to provide very high precision timer facility. However, the fact is that the operating system will run ONLY on that specific hardware. Such operating systems are called RTOS (Real-Time Operating System). However, Windows was designed to run on many, many different types of hardware. Clearly, one hardware will be different from the other in numerous ways and there's no way Windows will have intimate knowledge of every type of hardware. This leads to Windows not being able to give you such high precision timer like an RTOS will do.

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(): 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.
 
Share this answer
 
v5

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.

 
Share this answer
 
wrote:
C# or C++. How can I do that using Visual Studio 2008?


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.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900