VERY accurate timing

This is a discussion on VERY accurate timing within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hello, I am using Borland C++ 3 (yeayea oldie, need it for school) in MSDOS and trying to do VERY ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    5

    Question VERY accurate timing

    Hello,

    I am using Borland C++ 3 (yeayea oldie, need it for school) in MSDOS and trying to do VERY accurate timing.

    I need to get the time in milliseconds or more accurate. The clock() function divided by CLK_TCK divided by 1000 does NOT count milliseconds, ts not accurate enough, I tested that.

    Does anyone know a way to get REAL milliseconds or better?Maybe some assembly routine?

    Thanks!
    Last edited by Gherkin; 09-28-2001 at 09:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    You'll probably need to do some reading up on the programmable interval timer (PIT) which is used on PCs to control time related activities.

    This code reads the PIT...
    Code:
    #include <stdio.h>
    // for inp() - might also be called inportb or inb or something like that
    // could also be in a different .h file
    #include <pc.h>
    
    unsigned int read_pit ( void ) {
        // a 16 bit value, obtained in lsb/msb order
        unsigned char lsb = inp( 0x40 );
        unsigned char msb = inp( 0x40 );
        return lsb + ( msb << 8 );
    }
    int main ( ) {
        unsigned int a, b;
        int i;
        double elapsed;
    
        a = read_pit();
        for ( i = 0 ; i < 1000000 ; i++ ) {
            // do stuff to be timed
            // this has at most one rollover on my machine...
        }
        b = read_pit();
        printf( "%u %u\n", a, b );
        if ( a < b ) {
            // rollover adjustment
            // this assumes at most once - see below...
            a += 0x10000;
        }
        elapsed = (a-b) / 1193181.0;    // in fractions of a second
        printf( "%f seconds\n", elapsed );
        return 0;
    }
    The PIT counts down at a rate of 1193181 per second, which when divided by 65536 (max short int), gives the 18.2 ticks per second you may have seen around.

    But you'll also need to trap the timer interrupt to record how many times the timer rolls over from 0x0000 to 0xFFFF.

    This may help...
    http://www.ctyme.com/intr/rb-0043.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    mmm that rollover is nasty, never created such handlers, but ill try something and crash my system a couple times again, hehe
    Last edited by Gherkin; 09-28-2001 at 05:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counter (Time)
    By PickleBranston in forum C Programming
    Replies: 16
    Last Post: 02-10-2009, 01:00 PM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 04:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21