Solved

Millisecond timing of a loop.

Posted on 2002-05-27
6
413 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 and use conditional statements in the C programming language.

813 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

11 Experts available now in Live!

Get 1:1 Help Now