2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

floatとdoubleは結局どっちが早いのか良く分かりません。

Last updated at Posted at 2019-08-29

概要

floatとdoubleは結局どっちが早いのか良く分かりません。

サンプル

そもそもこんな書き様でどっちが早いとか言えるのか?すら分かりません。

# include <math.h>
# include <stdio.h>
# include <stdlib.h>
# include "windows.h"

# define C 4
# define N 1000000

int main()
{
	int i;
	float f;
	double d;
	double time;

	LARGE_INTEGER start, end, freq;
	QueryPerformanceFrequency(&freq);

	for (i = 0; i < C; i++)
	{
		f = 0;
		d = 0;
		QueryPerformanceCounter(&start);
		switch (i)
		{
		case 0:
			for (f = 1; f < N; f++) f += f * f / f - f;
			break;
		case 1:
			for (d = 1; d < N; d++) d += d * d / d - d;
			break;
		case 2:
			for (f = 1; f < N; f++) f += sinf(f) + logf(f) + sqrtf(f);
			break;
		case 3:
			for (d = 1; d < N; d++) d += sin(d) + log(d) + sqrt(d);
			break;
		default:
			break;
		}
		QueryPerformanceCounter(&end);
		time = (end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
		printf("%lfms\n", time);
		printf("float  %f\n", f);
		printf("double %lf\n", d);
		printf("\n");
	}
}

環境

VisualStudio 2019
Windows 10
Core i7 7700K 4.2GHz
x64

最適化O2

image.png

最適化Od

image.png

なにしてんの?

いや、floatの方がめっちゃ早い(数倍差)という状況に遭遇したんですよ。別に特殊な組み込み環境とかじゃなくて、普通のWindows環境だったんですよ?意味わからなかったんですよ。ふぁー???

参考

x86でdoubleがfloatより速いかどうかを検証してみた
https://qiita.com/telmin_orca/items/455dab4a6ffd4545ee95

【雑談】 floatはdoubleより高速だと考えている人はどれぐらいいますか?
https://dixq.net/forum/viewtopic.php?t=5259

doubleとfloatの計算速度の話
http://precure-3dprinter.hatenablog.jp/entry/2017/05/10/double%E3%81%A8float%E3%81%AE%E8%A8%88%E7%AE%97%E9%80%9F%E5%BA%A6%E3%81%AE%E8%A9%B1

float型とdouble型を比較した場合、常にfloatが速いと思ってはダメらしい
http://nakamura001.hatenablog.com/entry/20090226/1235641233

float型対double型
http://www.pro.or.jp/~fuji/mybooks/cdiag/cdiag.4.4.html

float型とdouble型のパフォーマンステスト
http://cppdiary.blog76.fc2.com/blog-entry-37.html

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?