Solved

C++ Timer

Posted on 1998-07-21
5
215 Views
Last Modified: 2011-09-20
I am currently writing a program using Visual C++ under Windows 95.
I would like to be able to time a peripherial device down to as accurate a time as possible, if possible in micro seconds. Is there any functions / techniques that will be able to give me this accurate a method of timing.

This program has to be accurate on various machines with different clock speeds.
The only solution I can find is the Performance Counter, but this is not always supported.
0
Comment
Question by:vally
5 Comments
 

Author Comment

by:vally
ID: 1168214
Edited text of question
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1168215
You'll need to do a couple of things:

Use the multimedia timer: timeSetEvent().

However, timeSetEvent is not really accurate, so that you can do is use timeSetEvent for a one-time event, 1 millisecond before the actual time. Then when the timer fires, use QueryPerformanceCounter() to count until the actual time has occurred.
0
 

Author Comment

by:vally
ID: 1168216
When I said time it down, I meant as in how long it took to finish its task, eg transfer a block of data.

Besides QueryPerformanceCounter is out as it is not always supported
0
 
LVL 11

Expert Comment

by:alexo
ID: 1168217
QueryPerformanceCounter() is supported on Pentuims and above, Pentium clones, Alphas, etc.  What more do you need?

Thui, your answer sounds strangely familiar :-)
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1168218
Use the timeGetTime function. The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.

DWORD dwStartTime = ::timeGetTime();
.
DWORD dwEndTime = ::timeGetTime();

DWORD dwHowLong = dwEndTime - dwStartTime;

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 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

17 Experts available now in Live!

Get 1:1 Help Now