I was looking at some stuff and I found a function from quake III sources written by John Carmack which return the square root of a float. It's suposed to be 4 times faster than the standart sqrt function from math.h
I checked in Irrlicht engine and this function is not used.
I made a bench of the John's function, and what surprised me is it's slower than the standart sqrt function... How can this be possible
The code :
Code: Select all
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <math.h>
float SquareRootFloat(float number) {
long i;
float x, y;
const float f = 1.5F;
x = number * 0.5F;
y = number;
i = * ( long * ) &y;
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( f - ( x * y * y ) );
y = y * ( f - ( x * y * y ) );
return number * y;
}
int main() {
float a=clock();
for(unsigned int i=0; i<100000000; i++) {
SquareRootFloat(4.0);
}
std::cout<<"With fast sqrt : "<<(clock()-a)<<std::endl;
a=clock();
for(unsigned int j=0; j<100000000; j++) {
sqrt(4.0);
}
std::cout<<"With stantart sqrt : "<<(clock()-a)<<std::endl;
system("PAUSE");
}
[/code]
EDIT : I tryed with compiler optimisation to the max.
The carmack function go from 1.7s to 0.78 as standart sqrt function
Lets test for 10^10 iterations...
Result : Carmack & standart spend same time to calculate sqare roots :S