Calculate the Clock Rate of your PC

by Paul Hsieh
Ever wonder if the MHz of your Pentium is what your salesman said it is? Or do you just need to know the precise speed of your computer? The following is a WATCOM C/C++ program that measures the speed of your PC by comparing the Pentium's cycle counter to the real time system clock.
#include <stdio.h>
#include <time.h>

// WARNING!! Pentium only!

int RDTSC(void);
#pragma aux RDTSC = ".586" "rdtsc" "shr eax, 8" "shl edx, 24" "or eax, edx" modify [eax edx] value [eax];

volatile time_t t;

main(int argc, char * argv[]) {
unsigned int cyclemark1;
unsigned int cyclemark2;
unsigned int dt;

    if(argc>1) sscanf(argv[1],"%d",&dt);
    else dt = 5;

    printf( "Please wait, this program takes about %d seconds to run.\n",dt+1 );

    t = time(NULL);
    while( t==time(NULL) );
    t = time(NULL);

    cyclemark1 = RDTSC();
    while( dt+t>time(NULL) );
    cyclemark2 = RDTSC();

    printf( "Your Pentium compatible's clock rate is %g Mhz\n",
            ((double)(cyclemark2 - cyclemark1))/((double)1000000.0/256.0)/((double)dt));


home programming mail me

Valid HTML 4.0!