Solved

windows NT timers

Posted on 1998-01-28
3
740 Views
Last Modified: 2013-12-03
I have a routine called gettimeofday defined like this:
gettimeofday(struct timeval *tp, struct timezone *tzp)
{
   SYSTEMTIME nt_time;
   LARGE_INTEGER liCounter;

   GetSystemTime( &nt_time );
   QueryPerformanceCounter (&liCounter);

   tp->tv_sec = nt_time.wSecond;
   tp->tv_usec = liCounter.LowPart;
}

I use this routine to get a diff of times. For example, let's say I want to time how long it takes me to "eat." So I do a gettimeofday() before I start "eating", I "eat", and then I do a gettimeofday() when I'm done "eating." then to get a "time to eat", I subtract the start time from the finish time.

My question is this: on some NT 4.0 systems, I get the diff time as 2 milliseconds, on some other NT 4.0 system, the diff time is 98000 milliseconds.

I don't understand why the same gettimeofday() routine will have such a big timer difference.

What's going on here?
0
Comment
Question by:rupalt
3 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1410547
This is a problem with multitasking systems. Since your process may not have the entire CPU time for the duration of "eating", the time it takes to eat will vary depending on what else is currently running. So the best way to figure out how long it takes to eat, is to eat many times and average out the result. Make sure you're eating alone too!
0
 

Author Comment

by:rupalt
ID: 1410548
Thanx Thui for answering my question. Actually, what I'm trying to do is measure response time. So what I do is I send out a packet from my host to the host I want to measure the response time of. When I send out that packet I timestamp it. That other host, send me a reply (within 2 milliseconds), the host that sent out the packet receives a reply, and checks the time. Then it does the diff of the two time to get the response time.

I checked for other processes besides the one doing the response time, and on the NT box, I have no other process running (NT might be doing "things", but I don't have any other process running, not even the DOS prompt.) I can't understand why the same system sending out the packet and timestamping and receiving the packet and timestamping would have such a huge time different of 2 milliseconds on some systems to 98000 on other systems.  Strangly, the system that's computing the 2 milliseconds response time (the correct response time) has so many other processes running than the one reporting 98000 milliseconds.
0
 
LVL 2

Accepted Solution

by:
anichini earned 100 total points
ID: 1410549
You aren't using QueryPerformanceCounter correctly. QueryPerformanceCounter does not return microseconds, it returns the number of "clock ticks". QueryPerformanceFrequency returns how many of these "clock ticks" there are per second. You need to have code like this:

#define TIME_START 0
#define TIME_END 1

LARGE_INTEGER liFreq, liValue[2];

QueryPerformanceFrequency(&liFreq);

QueryPerformanceCounter(&liValue[TIME_START]);

// ... do whatever you are timing

QueryPerformanceCounter(&liValue[TIME_END]);

// this code assumes that LONGLONG is a valid type
// I know for a fact on VC++ it is.

double secDiff, microsecDiff;
secDiff = double(liValue[TIME_END].QuadPart - liValue[TIME_START].QuadPart)/double(liFreq.QuadPart);
microsecDiff = secDiff * 1000 * 1000; // 1000 milliseconds in a second, 1000 microseconds in a millisecond

For the curious, the way MS usually implements QueryPerformanceCounter on Pentium-class machines and up is based on the clock frequency of the processor. therefore, faster processors have more precise timers (and QueryPerformanceFrequency should return larger numbers).


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

776 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