Solved

Accurate Timing in Windows

Posted on 1999-01-29
10
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 135
FMX TCameraComponent Problem 2 105
How to Correctly derive class from CWinThread in MFC 4 115
maximize the sum of fractions 33 74
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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