ich habe mich letzte Woche mal mit Optimierung etwas beschäftigt.
In meinem Programm vergleiche ich relativ oft Floats miteinander:
Code: Alles auswählen
float a=10.5;
float b=10.5;
const float eps = 1.0e-10;
if( fabs(a-b)>eps)
floatSindUngleich();
else
floatSindGleich();
Code: Alles auswählen
bool AlmostEqual2sComplement(float A, float B, int maxUlps)
{
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographically ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}
Das eigenartige dabei ist:
In der Debug-Version war der 2.Methode gesamt gesehen schneller, ~20%. .... habe mich schon gefreut.
In der ReleaseVersion war es andersherum, die 1.Methode war schneller..... :shock: .
Nunmal meine Frage:
Wie kann das sein?