Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accurate Timing in Windows

Posted on 1999-01-29
10
Medium Priority
?
420 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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