Thread: VERY accurate timing

  1. #1
    Registered User
    Join Date
    Sep 2001

    Question VERY accurate timing


    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?

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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...
    #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...
    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.

  3. #3
    Registered User
    Join Date
    Sep 2001
    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 04: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, 03: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, 12:43 AM