Solved

windows NT timers

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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