[C語言]CPU跑分程序


|版權聲明:本文為博主原創文章,未經博主允許不得轉載。

Code:

  1 #include <stdio.h>
  2 #include <conio.h>
  3 #include <time.h>//clock()所屬頭文件
  4 const int N_qsort=10000;//快排的數據規模
  5 const int M=20000,N=50000;//整點、浮點運算的規模
  6 const int N_pi=100000000;//計算圓周率的運算規模
  7 double s_int,s_float,s_pi,s_sort;
  8 void int_comp(void);//整點運算
  9 void float_comp(void);//浮點運算
 10 void pi_comp(void);//泰勒級數推論式計算圓周率
 11 void Qsort(int a[],int low,int high);//快排算法
 12 void qsort(void);//調用快排算法的函數
 13 void panduan();
 14 void PAUSE();
 15 int main(){
 16      printf("------\n性能測試開始\n");
 17      int_comp();//整點運算
 18      float_comp();//浮點運算
 19      pi_comp();//泰勒級數推論式計算圓周率
 20      qsort();//快速排序
 21      printf("------\n測試結束\n");
 22      printf("整點運算得分:%lf\n",s_int);
 23      printf("泰勒級數推論式計算圓周率運算得分:%lf\n",s_pi);
 24      printf("排序運算得分:%lf\n",s_sort);
 25      printf("總得分:%lf\n",s_int+s_float+s_pi+s_sort);
 26      panduan();
 27      PAUSE();
 28      return 0;
 29 }
 30 
 31 void int_comp(void){//整點加法
 32      printf("整點運算測試中(運算次數為:%lf)\n",(double)M*N);
 33      clock_t start,end;
 34      int i,j;
 35      start=clock();
 36      for(i=0;i<M;i++)
 37          for(j=0;j<N;j++);
 38      end=clock();
 39      double duration=(double)(end-start)/CLOCKS_PER_SEC;
 40      double score=(M*N)/duration;
 41      /*注:score本身即為運算速度,數量級一般在億,為方便起見,本程序的分數均采用運算速度除以一萬后的結果!除特殊說明,后面類同!*/
 42      s_int=score/10000;
 43      //printf("整點運算測試完畢!分數:%lf\n",s_int);
 44 }
 45 
 46 void float_comp(void){//浮點加法
 47      printf("浮點運算測試中(運算次數為:%lf)\n",(double)M*N);
 48      clock_t start,end;
 49      float i,j;
 50      start=clock();
 51      for(i=0;i<M;i++)
 52      for(j=0;j<N;j++);
 53      end=clock();
 54      double duration=(double)(end-start)/CLOCKS_PER_SEC;
 55      double score=(M*N)/duration;
 56      s_float=score/10000;
 57      //printf("浮點運算測試完畢!分數:%lf\n",s_float);
 58 }
 59 
 60 void pi_comp(void){
 61      printf("泰勒級數推論式計算圓周率中(運算次數為:%d)\n",N_pi);
 62      int m,i=1;
 63      double s=0;
 64      clock_t start,end;
 65      start=clock();
 66      for(m=1;m<N_pi;m+=2){
 67         s+=i*(1.0/m);
 68         i=-i;
 69      }
 70      end=clock();
 71      double duration=(double)(end-start)/CLOCKS_PER_SEC;
 72      double score=N_pi/duration;
 73      //下面一行可輸出計算出來的圓周率
 74      //printf("pi=%lf\n",4*s);
 75      s_pi=score/10000;
 76      //printf("泰勒級數推論式計算圓周率完畢!分數:%lf\n",s_pi);
 77 }
 78 void Qsort(int a[],int low,int high){//快排算法
 79      if(low>=high) return;
 80      int first=low;
 81      int last=high;
 82      int key=a[first];
 83      while(first<last){
 84          while(first<last&&a[last]>=key) --last;
 85          a[first]=a[last];
 86          while(first<last&&a[first]<=key) ++first;
 87          a[last]=a[first];
 88      }
 89      a[first]=key;
 90      Qsort(a,low,first-1);
 91      Qsort(a,first+1,high);
 92 }
 93 void qsort(void){//調用快排算法的函數
 94      int a[N_qsort],i;
 95      for(i=N_qsort;i>0;i--) a[N_qsort-1]=i;
 96      printf("排序運算中(對%d個數進行快速排序)\n",N_qsort);//采用最壞時間方案
 97      clock_t start,end;
 98      start=clock();
 99      Qsort(a,0,N_qsort-1);
100      end=clock();
101      double duration=(double)(end-start)/CLOCKS_PER_SEC;
102      double score=(N_qsort*N_qsort)/duration;
103      s_sort=score/10000;
104 //printf("排序運算測試完畢!分數:%lf\n",s_sort);
105 }
106 void panduan(){
107     float i=s_int+s_float+s_pi+s_sort;
108     printf("根據分數,授予您的愛機<");
109     if (i>0&&i<20000){
110         printf("渣渣");
111     }
112     else if (i>20000&&i<30000){
113         printf("低端");
114     }
115     else if (i>30000&&i<40000){
116         printf("中端");
117     }
118     else if (i>40000&&i<50000){
119         printf("高端");
120     }
121     else if (i>50000&&i<60000){
122         printf("超高端");
123     }
124     else if (i>60000){
125         printf("機皇");
126     }
127     printf(">稱號\n");
128 }
129 void PAUSE(){
130     printf("\n請按任意鍵繼續...");getch();fflush(stdin);
131 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM