//------------------------------------------------------------------- // timedgcd.cpp (revised version of our 'gcdcalc.c') // // This program accepts two integers as command-line arguments, // then it computes and displays their greatest common divisor. // // compile using: $ gcc fastgcd.s timedgcd.s -o timedgcd // execute using: $ ./timedgcd // // programmer: ALLAN CRUSE // written on: 29 MAR 2006 // revised on: 03 APR 2006 -- to perform a timing comparison //------------------------------------------------------------------- #include // for printf() #include // for atoi() extern "C" int gcd( int, int ); unsigned long long tsc( void ) { asm(" rdtsc" ); } unsigned long long stamp0, stamp1, stamp2, stamp3; int gcd1( int m, int n ) { int p = ( m < 0 ) ? -m : m; int q = ( n < 0 ) ? -n : n; while ( q != 0 ) { int r = p % q; p = q; q = r; } return p; } int main( int argc, char **argv ) { if ( argc < 3 ) return 1; int m = atoi( argv[1] ); int n = atoi( argv[2] ); stamp0 = tsc(); int g1 = gcd1( m, n ); stamp1 = tsc(); stamp2 = tsc(); int g2 = gcd( m, n ); stamp3 = tsc(); printf( "\n\n\t gcd1( %d , %d ) = %d ", m, n, g1 ); printf( " gcd( %d , %d ) = %d \n\n", m, n, g2 ); printf( "\tstamp1 = %20llu stamp3 = %20llu \n", stamp1, stamp3 ); printf( "\tstamp0 = %20llu stamp2 = %20llu \n", stamp0, stamp2 ); printf( " cpu-cycles = %20llu ", stamp1 - stamp0 ); printf( " %20llu \n\n", stamp3 - stamp2 ); }