Solved

Millisecond timing of a loop.

Posted on 2002-05-27
6
414 Views
Last Modified: 2010-04-15
Hi I've got a device hooked up to the parallel port and I want to see how long it outputs a "low" signal.  This is my program so far:

#include <stdio.h>

void main () {
  int in;
  in = inp(889);
  for(;in == 120;in = inp(889)){
  };
  // Start of Timer.
  for(;in != 120;in = inp(889)){
  };
  // End of Timer.
};

So quite simply all I'm trying to do is measure the length of time (in milliseconds) the second for loop lasts.  Trouble is I don't know how to measure time in milliseconds.  Can anybody help me out with some code to count this for loop in milliseconds?

Thanks in advance,
Cide
0
Comment
Question by:Cide
6 Comments
 

Author Comment

by:Cide
ID: 7037553
Sorry forgot to say I'm using djgpp and windows 98.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7038567
If you're writing this as a Win32 console application you can use the function timeGetTime() (found in WINMM.LIB) to return a millisecond-accurate timer. (Well, it SHOULD be millisecond accurate--I think under Win32 this function is actually getting its information from a separate timer thread, so it's possible for a lot of high-priority system threads to kick the value out of sync).

If you want this to be a pure DOS application then I haven't a clue how you'd get millisecond accurate timing.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7038569
Oh, if you want the highest accuracy, you might also want to look at the QueryPerformanceCounter() and QueryPerformanceFrequency() functions--you'll need to use 64-bit arithmetic to handle the return values in this case, though.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 6

Accepted Solution

by:
zebada earned 200 total points
ID: 7038992
Like pjknibbs said, you should use QueryPerformanceCounter().
This is C++ code but it simple to convert to C code.

#include <windows.h>
#include <stdio.h>

#define unsigned long ULONG;

class hrt {
private:
  LARGE_INTEGER frequency;

  LARGE_INTEGER startCount;
  SYSTEMTIME    startTime;
  LARGE_INTEGER startMs;

  LARGE_INTEGER currentCount;
  SYSTEMTIME    currentTime;
  LARGE_INTEGER currentMs;

public:
  hrt(void);
  ~hrt(void){};

  SYSTEMTIME *getTime();
};

hrt::hrt()
{
  QueryPerformanceFrequency(&frequency);
  QueryPerformanceCounter(&startCount);
  GetSystemTime(&startTime);
  startMs.QuadPart = startTime.wHour*3600000+startTime.wMinute*60000+startTime.wSecond*1000+startTime.wMilliseconds;
}

SYSTEMTIME *
hrt::getTime()
{
  ULONG ms;
  ULONG hour,min,sec;

  QueryPerformanceCounter(&currentCount);
  currentMs.QuadPart = ((currentCount.QuadPart-startCount.QuadPart)*1000)/frequency.QuadPart;
  ms = static_cast<ULONG>(startMs.QuadPart+currentMs.QuadPart);

  hour = ms/3600000;
  ms %= 3600000;
  min = ms/60000;
  ms %= 60000;
  sec = ms/1000;
  ms %= 1000;

  currentTime = startTime;
  currentTime.wHour = static_cast<USHORT>(hour);
  currentTime.wMinute = static_cast<USHORT>(min);
  currentTime.wSecond = static_cast<USHORT>(sec);
  currentTime.wMilliseconds = static_cast<USHORT>(ms);
  return &currentTime;
}

int
main(int argc, char *argv[])
{
  hrt         t;
  SYSTEMTIME  *now;

  for ( int i=0 ; i<100 ; i++ ) {
    now = t.getTime();
    printf("%02d:%02d:%02d.%03d\n",now->wHour,now->wMinute,now->wSecond,now->wMilliseconds);
  }
  return 0;
}
0
 
LVL 9

Expert Comment

by:BeyondWu
ID: 7043420
You can also simply use GetTickCount().
0
 
LVL 6

Expert Comment

by:zebada
ID: 7043489
No, you can't use GetTickCount() as the resolution is not high enough.

Windows NT 3.5 and later The system timer runs at approximately 10ms.
Windows NT 3.1 The system timer runs at approximately 16ms.
Windows 95 and later The system timer runs at approximately 55ms.

0

Featured Post

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

830 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