Solved

Accurate Timing in Windows

Posted on 1999-01-29
10
400 Views
Last Modified: 2010-08-05
Is there a way to guarantee accurate time (mlliseconds) from a C++ program running in a windows environment?
0
Comment
Question by:zilch
10 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 200 total points
ID: 1185505
You can always call GetTickCount() at the start of the program and call it again at the end to determine the number of milliseconds that have expired.

This is guaranteed to work because GetTickCount() returns the number of milliseconds that have elapsed since the start of Windows. However, the number of milliseconds elapsed for your program may be inaccurate because of the dynamics of the OS.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1185506
To retrieve the time your program consumed, 'GetProcessTimes()' would do the job better...
0
 
LVL 23

Expert Comment

by:chensu
ID: 1185507
The GetTickCount function is limited to the resolution of the system timer. If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

The following functions are used with multimedia timers.

timeBeginPeriod  
timeEndPeriod  
timeGetDevCaps  
timeGetSystemTime  
timeGetTime  
timeKillEvent  
TimeProc  
timeSetEvent

The following functions are used with high-resolution timers.

QueryPerformanceFrequency
QueryPerformanceCounter


You may use timeGetTime instead of GetTickCount.

"Windows NT: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution,

Windows 95: The default precision of the timeGetTime function is 1 millisecond. In other words, the timeGetTime function can return successive values that differ by just 1 millisecond. This is true no matter what calls have been made to the timeBeginPeriod and timeEndPeriod functions."

0
 

Author Comment

by:zilch
ID: 1185508
I should have stated more throughly.  I need a way to generate accurate time events within a Windows environment, not total time running!  This is for controlling turn on/off events for testing, as well as measuring total times of the events.  This is regardless of the mouse event or any other interrupt windows might do.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1185509
So, use timeSetEvent to set a multimedia timer.

By the way, you should have rejected the answer since it doesn't solve your problem.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Expert Comment

by:CameronP
ID: 2351989
chensu;

I have a problem with timeGetTime.  I tried it using the mmsystem.h included and I still got a linker error.  Is there a special setting I have to set when using this function?

By the way zilch, why did you accept this answer if it was'nt correct.  I gave up 20 points to get to it and then found nothing concrete.

Cheers
Cameron
0
 
LVL 23

Expert Comment

by:chensu
ID: 2353928
You need to link with winmm.lib.
0
 

Expert Comment

by:CameronP
ID: 2358443
Thanks.

I linked it and its fine now, but is the timeGetTime value actually accurate to 1mS?  I am trying to switch some relays which uses some multiplexing which rely on accurate time switching.

Cheers
Cameron
0
 
LVL 23

Expert Comment

by:chensu
ID: 2359973
Windows NT: The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum difference between successive values returned by timeGetTime can be as large as the minimum period value set using timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution.

Windows 95: The default precision of the timeGetTime function is 1 millisecond. In other words, the timeGetTime function can return successive values that differ by just 1 millisecond. This is true no matter what calls have been made to the timeBeginPeriod and timeEndPeriod functions.
0
 

Expert Comment

by:CameronP
ID: 2361753
Thankyou very much.

Cheers
Cameron
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now