CPU安定性の計算

1. Cソースコード(cpustbench.c)

  • 全点演算、浮動小数点演算、PI演算、およびクイックソート演算を通じてCPU安定性の計算実行します
  • CLOCKS_PER_SECは、CPUの実行時間を取得するために実行されているCPUの1秒あたりのクロックサイクル数(1000)です。
#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
#endif

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 );

#endif


int main()
{
    
    
	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);
}


void int_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;
}


void float_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;
}


void pi_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;
}


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

2つ目は、Linuxでコンパイルして実行することです。

  • デフォルトのコンパイル
gcc cpustbench.c -o cpustbench.o
  • パラメータを使用したコンパイル(任意の数のパラメータ)
gcc cpustbench.c -DInteger_Scale=30000 -DFloat_Scale=60000 -DPI_Scale=100000000 -DQsort_Scale=20000 -DNTIMES=3 -o cpustbench.o

3、コマンドを実行します

./cpustbench.o

4、実行結果を表示します

cat /tmp/cpudata

おすすめ

転載: blog.csdn.net/LvJzzZ/article/details/112004091