Solved

windows NT timers

Posted on 1998-01-28
3
738 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

19 Experts available now in Live!

Get 1:1 Help Now