標籤:結束 快排 ons clean 速度 std star 效能測試 數量級
#include <stdio.h>#include <time.h>//clock()所屬標頭檔const int N_qsort=10000;//快排的資料規模const int M=20000,N=50000;//整點、浮點運算的規模const int N_pi=100000000;//計算圓周率的運算規模double s_int,s_float,s_pi,s_sort;void int_comp(void);//整點運算void float_comp(void);//浮點運算void pi_comp(void);//泰勒級數推論式計算圓周率void Qsort(int a[],int low,int high);//快排演算法void qsort(void);//調用快排演算法的函數void panduan();void PAUSE();int main(){ printf("------\n效能測試開始\n"); int_comp();//整點運算 float_comp();//浮點運算 pi_comp();//泰勒級數推論式計算圓周率 qsort();//快速排序 printf("------\n測試結束\n"); printf("整點運算得分:%lf\n",s_int); printf("泰勒級數推論式計算圓周率運算得分:%lf\n",s_pi); printf("排序運算得分:%lf\n",s_sort); printf("總得分:%lf\n",s_int+s_float+s_pi+s_sort); panduan(); PAUSE();}void int_comp(void){//整點加法 printf("整點運算測試中(運算次數為:%lf)\n",(double)M*N); clock_t start,end; int i,j; start=clock(); for(i=0;i<M;i++) for(j=0;j<N;j++); end=clock(); double duration=(double)(end-start)/CLOCKS_PER_SEC; double score=(M*N)/duration; /*註:score本身即為運算速度,數量級一般在億,為方便起見,本程式的分數均採用運算速度除以一萬後的結果!除特殊說明,後面類同!*/ s_int=score/10000; //printf("整點運算測試完畢!分數:%lf\n",s_int);}void float_comp(void){//浮點加法 printf("浮點運算測試中(運算次數為:%lf)\n",(double)M*N); clock_t start,end; float i,j; start=clock(); for(i=0;i<M;i++) for(j=0;j<N;j++); end=clock(); double duration=(double)(end-start)/CLOCKS_PER_SEC; double score=(M*N)/duration; s_float=score/10000; //printf("浮點運算測試完畢!分數:%lf\n",s_float);}void pi_comp(void){ printf("泰勒級數推論式計算圓周率中(運算次數為:%d)\n",N_pi); int m,i=1; double s=0; clock_t start,end; start=clock(); for(m=1;m<N_pi;m+=2){ s+=i*(1.0/m); i=-i; } end=clock(); double duration=(double)(end-start)/CLOCKS_PER_SEC; double score=N_pi/duration; //下面一行可輸出計算出來的圓周率 printf("pi=%lf\n",4*s); s_pi=score/10000; //printf("泰勒級數推論式計算圓周率完畢!分數:%lf\n",s_pi);}void Qsort(int a[],int low,int high){//快排演算法 if(low>=high) return; int first=low; int last=high; int key=a[first]; while(first<last){ while(first<last&&a[last]>=key) --last; a[first]=a[last]; while(first<last&&a[first]<=key) ++first; a[last]=a[first]; } a[first]=key; Qsort(a,low,first-1); Qsort(a,first+1,high);}void qsort(void){//調用快排演算法的函數 int a[N_qsort],i; for(i=N_qsort;i>0;i--) a[N_qsort-1]=i; printf("排序運算中(對%d個數進行快速排序)\n",N_qsort);//採用最壞時間方案 clock_t start,end; start=clock(); Qsort(a,0,N_qsort-1); end=clock(); double duration=(double)(end-start)/CLOCKS_PER_SEC; double score=(N_qsort*N_qsort)/duration; s_sort=score/10000;//printf("排序運算測試完畢!分數:%lf\n",s_sort);}void panduan(){ float i=s_int+s_float+s_pi+s_sort; printf("根據分數,授予您的愛機<"); if (i>0&&i<20000){ printf("渣渣"); } else if (i>20000&&i<30000){ printf("低端"); } else if (i>30000&&i<40000){ printf("中端"); } else if (i>40000&&i<50000){ printf("高端"); } else if (i>50000&&i<60000){ printf("超高端"); } else if (i>60000){ printf("機皇"); } printf(">稱號\n");}void PAUSE(){ clean_stdin();}
純C語言跑分(詳細注釋)