のんびりしているエンジニアの日記

ソフトウェアなどのエンジニア的な何かを書きます。

C++ math.h 主な関数の速度比較(exp,sin,sqrt,pow,log,acosh)

Sponsored Links

皆さんこんにちわ
お元気ですか?私はしんどいです。

さて、本日はCの数学関数を使います。
色々と計算に便利なこの関数ですが、速度はいかほどに…

実験

実験すること。

1000000000回計算するだけの簡単なお仕事です。

実験環境

OS:X 10.9.1
CPU:2.8GHz IntelCore i7
メモリ:16GB 1600MHz DDR3

実験内容

アクセス, exp, sin, sqrt, pow, log, acoshの速度を比較する

実験結果

内容 速度
access 2.09s
exp 2.09s
sin 2.06s
sqrt 2.1s
pow 2.09s
log 2.09s
acosh 36s

感想

acoshだけは遅すぎる…。
アクセス速度と比較しても大差ないのか。。

ソースコード

#include <iostream>
#include <math.h>

#define N 1000000000

using namespace std;

int arr[1000000000];

void t_access(){
	for(int i = 0; i < N; i++){
		arr[i];
	}
}

void t_exp(){
	for(int i = 0; i < N; i++){
		exp(100);
	}
}

void t_sin(){
	for(int i = 0; i < N; i++){
		sin(M_PI);
	}
}

void t_sqrt(){
	for(int i = 0; i < N; i++){
		sqrt(100);
	}
}

void t_pow(){
	for(int i = 0; i < N; i++){
		pow(100,8.0);
	}
}

void t_log(){
	for(int i = 0; i < N; i++){
		log(100);
	}
}

void t_acosh(){
	for(int i = 0; i < N; i++){
		acosh(100);
	}
}

int main(void){
	for(int i = 0; i < N; i++){
		arr[i] = 100;
	}

	void (*po[])() = {t_access,t_exp,t_sin,t_sqrt,t_pow,t_log,t_acosh};
	for(int i = 0; i < 7; i++){
		clock_t start,end;
		start = clock();
		(*po[i])();
		end = clock();
		cout << (double)(end - start) / CLOCKS_PER_SEC << endl;
	}
	return 0;
}