Solved

C++ Timer

Posted on 1998-07-21
5
243 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
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.

803 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