Solved

Accurate Timing in Windows

Posted on 1999-01-29
10
410 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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