You are here:

C++/Accurate clock time between two events.


QUESTION: I am using Threading in MICROSOFT VC++ 6.0 in embedded application. I want to calculate the exact time difference between between two events. I tried clock(),time()and _ftime() but all the functions failed to calculate the accurate delay time. Since it is embedded project, system is very much conscious about time delay. Can u help?

ANSWER: Use GetTickCount for millisecond resolution (subtract the first reading from the second for elapsed milliseconds).

For higher resolution timing, use QueryPerformanceCounter, which you also need QueryPerformanceFrequency to know the time base.

For Windows, mS timing has been sufficient for me.  But for embedded PowerPC, I use the TimeBase counter and measure timing of events in 15nS resolution (which I use a uS for display).


---------- FOLLOW-UP ----------

QUESTION: I am working in Microsoft VC++ environment and developing VC++ code for scientific application. There is a requirement for time calculation in milliseconds for a particular task. I tried to calculate the time using clock( ) function in which the calculated time is not consistent, i.e. every time there is a variation from the actual. Also other functions like time() and _ftime( ) were used for calculation, I could not get the exact time.

I posted the problem to you and got the reply. You suggested to use GetTickCount() function. This function works properly as I expected. I know definition of clock(),time(),ftime() as given in MSDN.  But clock() , time(), _ftime() also have the same the definition like GetTickCount().  Then why the time value returned by clock() , time(), _ftime() is varying. Can I know exact difference between these functions?  

Although they have the same definition, where in the OS they get their values could (and shows to be is) very different.  I don't know the details of how they are different or how they are implemented, but I would guess that GetTickCount gets it's value from a timer-updated variable, while the others go through other OS layers for time-zone and locale processing.

For the exact differences, I think you need to find a message board which has Microsoft personnel participating.  Whoever knows the details has to know the operating system very well.



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 All rights reserved.