Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

windows NT timers

Posted on 1998-01-28
3
Medium Priority
?
746 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 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 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