[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Millisecond timing of a loop.

Posted on 2002-05-27
6
Medium Priority
?
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
zebada earned 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

649 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