Solved

windows NT timers

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Visual Fox Pro commands 15 50
Windows Mobile 6.5 device Apache Flex 3 48
wordpress limitations 4 124
Microsoft C++ code failing in executable that worked 9 108
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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