https://ko.wikipedia.org/wiki/%EA%B3%A0%EC%86%8D_%EC%97%AD_%EC%A0%9C%EA%B3%B1%EA%B7%BC
고속 역 제곱근 - 위키백과, 우리 모두의 백과사전
고속 역 제곱근(高速逆-根, fast inverse square root)은 때때로 Fast InvSqrt()나 16진수 0x5f3759df라고도 하는, IEEE 754 부동소수점 체계의 32비트 실수에 대한 제곱근의 역수를 계산하기 위한 알고리즘이다.
ko.wikipedia.org
- 고속 역제곱근을 통한 제곱근 연산 1000만회 vs math.h의 sqrtf 1000만회 -> 10회 반복
< 실험 코드 >
#include <iostream>
#include <Windows.h>
#include <time.h>
#include <vector>
#include <functional>
#include <string>
#include <list>
#include "Timer.h"
float FastSqrt(const float& n)
{
static union { int i; float f; } u;
u.i = 0x5F375A86 - (*(int*)&n >> 1);
return (int(3) - n * u.f * u.f) * n * u.f * 0.5f;
}
int main()
{
srand((unsigned int)time(0));
rand();
CTimer Timer;
int FastWin = 0;
int StdWin = 0;
for (int i = 0; i < 10; ++i)
{
Timer.StartTimer();
for (int i = 0; i < 10000000; ++i)
{
float ff = FastSqrt((float)rand());
}
float FastTime = Timer.EndTimer();
Timer.StartTimer();
for (int i = 0; i < 10000000; ++i)
{
float ff = sqrtf((float)rand());
}
float StdTime = Timer.EndTimer();
if (FastTime > StdTime)
++StdWin;
else
++FastWin;
printf("\n");
}
printf_s("\n\n * FASTSQRT WIN: %d\n * STDSQRT WIN: %d", FastWin, StdWin);
return 0;
}
< 결과 >
- 정확도를 약간 희생시키겠지만 고속 역제곱근이 평균적으로 빠르다.
- 외국의 다른 사이트에서 찾은 내용으로, 전처리 지시자를 바꾸면 기존 sqrt를 Fast Inv Sqrt보다 훨씬 빨라진다고 함.
https://www.linkedin.com/pulse/fast-inverse-square-root-still-armin-kassemi-langroodi
Is Fast Inverse Square Root still Fast?
Introduction Fast Inverse Square Root (Fast InvSqrt) is an algorithm that quickly estimates the inverse of the square root of a float variable. The algorithm appeared first in Quake III Arena first-person shooter game source code [1].
www.linkedin.com
'자습' 카테고리의 다른 글
TIPS (0) | 2022.08.20 |
---|---|
삼항 연산자 주의점 (0) | 2022.06.02 |
std::list의 begin()과 end() (0) | 2022.05.30 |
Notify 최적화 테스트 (0) | 2022.05.30 |
vector 자료구조에서도 iterator 사용 가능 (0) | 2022.05.23 |