2014年8月21日 星期四

執行時間測量(gcd)

#include <time.h>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int gcd1(int a, int b){
     for (int d = 1; d <= min(a, b); d++){
         if (a%d == 0 && b%d == 0) return d;
     }
}

int gcd2(int a, int b){
     if (a<b) swap(a, b);
     if (a%b)
         return b;
     else
         return gcd2(a - b, b);
}
// source: http://www.math.wustl.edu/~victor/mfmm/compaa/gcd.c
/* Standard C Function: Greatest Common Divisor */
int gcd(int a, int b)
{
     int c;
     while (a != 0) {
         c = a; a = b%ab = c;
     }
     return b;
}

/* Recursive Standard C Function: Greatest Common Divisor */
int gcdr(int a, int b)
{
     if (a == 0) return b;
     return gcdr(b%a, a);
}


void main(){
     clock_t begin = clock();
     // your CODE
     srand(clock());
     int x = rand();
     int y = rand();
     for (int i = 0; i < 100000000; i++){
         gcd1(x, y);
     }
     clock_t end = clock();  
     cout << end - begin;
     //system("pause");
}


沒有留言:

張貼留言