Realice cálculos de estabilidad de la CPU a través de operaciones de punto completo, operaciones de punto flotante, operaciones PI y operaciones de clasificación rápida
CLOCKS_PER_SEC es el número de ciclos de reloj por segundo (1000) de la CPU en ejecución para obtener el tiempo de ejecución de la CPU
#include<stdio.h>#include<time.h>#ifndef __CPU_TEST_#define __CPU_TEST_#ifdef Integer_Scale#if Integer_Scale <= 20000#define Integer_Scale 20000#endif#endif#ifndef Integer_Scale#define Integer_Scale 20000#endif#ifdef Float_Scale#if Float_Scale <= 50000#define Float_Scale 50000#endif#endif#ifndef Float_Scale#define Float_Scale 50000#endif#ifdef PI_Scale#if PI_Scale <= 100000000#define PI_Scale 100000000#endif#endif#ifndef PI_Scale#define PI_Scale 100000000#endif#ifdef Qsort_Scale#if Qsort_Scale <= 10000#define Qsort_Scale 10000#endif#endif#ifndef Qsort_Scale#define Qsort_Scale 10000#endif#ifdef NTIMES#if NTIMES <= 2#define NTIMES 5#endif#endif#ifndef NTIMES#define NTIMES 5#endifdouble s_int, s_float, s_pi, s_sort;voidint_comp(void);voidfloat_comp(void);voidpi_comp(void);voidQsort(int a[],int low,int high );voidqsort(void);#endifintmain(){
int k;
FILE *fp;
fp =fopen("/tmp/cpudata","w+");for( k =0; k < NTIMES; k++){
int_comp();float_comp();pi_comp();qsort();fprintf( fp,"%.3lf\t", s_int );fprintf( fp,"%.3lf\t", s_float );fprintf( fp,"%.3lf\t", s_pi );fprintf( fp,"%.3lf\t", s_sort );fprintf( fp,"%.3lf\n", s_int + s_float + s_pi + s_sort );}fclose( fp );return(0);}voidint_comp(void){
clock_t start, end;int i, j;
start =clock();for( i =0; i < Integer_Scale; i++)for( j =0; j < Float_Scale; j++);
end =clock();double duration =(double)(end - start)/ CLOCKS_PER_SEC;
s_int = duration;}voidfloat_comp(void){
clock_t start, end;float i, j;
start =clock();for( i =0; i < Integer_Scale; i++)for( j =0; j < Float_Scale; j++);
end =clock();double duration =(double)(end - start)/ CLOCKS_PER_SEC;
s_float = duration;}voidpi_comp(void){
int m, i =1;double s =0;
clock_t start, end;
start =clock();for( m =1; m < PI_Scale; m +=2){
s += i *(1.0/ m);
i =-i;}
end =clock();double duration =(double)(end - start)/ CLOCKS_PER_SEC;/*圆周率 *//* printf( "pi=%lf\n", 4 * s ); */
s_pi = duration;}voidQsort(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 );}voidqsort(void){
int a[Qsort_Scale], i;for( i = Qsort_Scale; i >0; i--)
a[Qsort_Scale -1]= i;
clock_t start, end;
start =clock();Qsort( a,0, Qsort_Scale -1);
end =clock();double duration =(double)(end - start)/ CLOCKS_PER_SEC;
s_sort = duration;}
Dos, compilar y ejecutar en Linux
Compilación predeterminada
gcc cpustbench.c -o cpustbench.o
Compilación con parámetros (cualquier número de parámetros)