From Newsgroup: comp.lang.c
fir pisze:
fir pisze:
fir pisze:
i measured also this hypot (i was not aware of this)
float-a sin_tests()
{
-a-a-a-a-afloat sum-a = 0;
-a-a-a-a-a if(w_pressed)-a-a-a-a-a for(int i=0;-a i<1000000; i++)
-a-a-a-a-a {
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += sqrt (a*a+b*b);
-a-a-a-a-a }
-a-a-a-a-a else
-a-a-a-a-a-a-a-a-a for(int i=0;-a i<1000000; i++)
-a-a-a-a-a-a-a-a-a {
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float a = 3*i/1000000.;
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a float b = 7*i/1000000.;
-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a sum += hypot (a, b);
-a-a-a-a-a-a-a-a-a }
-a-a-a-a-a-a return sum;
}
and what it showed sqrt being unexpectedly slower than sin (1) about
100 ms and hypot quite fast 5.5 ms
well bug mistake here sqrtf showed to be very fast (its even faster
than this 5.5 ms now its about 2.5 here) in turn what is tragically
slow it this hypot so this 100 ms is this hypot not sqrt..sqrt showed
totally fast (85 now)
i mean hypot 85 sqrt 2.5
ok the mystery soewhat resolved why i got sqrt before 5.5 and now 2.5 it
seem just be difference among sqrt and sqrtf )i didnt realized
probably double version is called when i pass a float
atan2 has even more difference aor atan2 65 ms/ns here
45 ms/ns for atan2f
those both fast atans
float fast_atan2_(float y, float x)
{
float ax = fabsf(x);
float ay = fabsf(y);
float a;
float r;
if(ax > ay)
{
r = ay / ax;
a = r * (1.0f - 0.2146018f * r * r);
}
else
{
r = ax / ay;
a = 1.5707963f - r * (1.0f - 0.2146018f * r * r);
}
if(x < 0) a = 3.14159265f - a;
if(y < 0) a = -a;
return a;
}
float fast_atan2(float y, float x)
{
// float PI = 3.14159265358979323846f;
float PI2 = 1.57079632679489661923f;
float ay = fabsf(y) + 1e-10f;
float r;
if(x >= 0)
{
r = (x - ay) / (x + ay);
r = PI2 * 0.5f - r * (0.9817f - 0.1963f * r * r);
}
else
{
r = (x + ay) / (ay - x);
r = 3.0f * PI2 * 0.5f
- r * (0.9817f - 0.1963f * r * r);
}
return y < 0 ? -r : r;
}
around 5.5 ms/ns
it seems i need to recheck the sin and now it is
sin 57 ms sinf 37 ms (about as its slightly oscillating)
previously it was around 70 ms as hypit was around 100 not 85
maybe it was becouse i was playin youtube in bacground while doin tests
i assumed it is on other core coz it didnt seem to interfere but
maybe that ould explain those differences
i also like got an observation like cpu would hold some global state
that changes but im not sure as sometimes like those primitives
could have a somewhat different times..not quite as i said this
suspiction on using second core slow things down but YET some global
state maybe fpu flags..i dont know
but the results are as i said (at least for my old cpu)
sqrtf - VERY FAST - USE
sinf/sin - SLOW - DONT USE (sinf eventually 37 is maybe not a tragedy
but not for time valuable code)
atan2/atanf - SLOW DONT USE
hypot - TERRIBLY SlOW DONT USE
tabelarized sin - VERY FAST - USE
algebraic atan2 - FAST - USE (not checked for error but those
sccumulative sums like 0.1 % difference, so not bad)
this makes all bothsin and sqrt logically almost unusable in my
applications..wonder how with atan2..but im to weary to test now
--- Synchronet 3.22a-Linux NewsLink 1.2