[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 }

 

posted @ 2016-10-16 00:19  林博士  阅读(2387)  评论(0编辑  收藏  举报