Benchmarking math.h square root and square root Quake

Ok, so I was on board and wondered how fast the square of the root of math.h was compared to the one that has a magic number in it (made famous by Quake, but made by SGI).

But it turned out to me in a world of resentment.

I first tried this on a Mac, where math.h would win hands every time, and then on Windows, where the magic number always won, but I think it all comes down to my own void.

  • Compiling on a Mac using "g ++ -o sq_root sq_root_test.cpp" takes about 15 seconds to start a program. But compilation in VS2005 at release takes a few seconds. (in fact, I had to compile in debugging just to display some numbers)

  • My bad man benchmarking? is it really stupid? cos I get 0.01 for math.h and 0 for the Magic number. (maybe it could be so fast?)

I don't know if that matters, but the Mac is Intel and the PC is AMD. Is a Mac using hardware for math.h sqroot?

I got the fast square root algorithm from http://en.wikipedia.org/wiki/Fast_inverse_square_root

//sq_root_test.cpp

#include <iostream>
#include <math.h>
#include <ctime>


float invSqrt(float x)
{
    union {
        float f;
        int i;
    } tmp;
    tmp.f = x;
    tmp.i = 0x5f3759df - (tmp.i >> 1);
    float y = tmp.f;
    return y * (1.5f - 0.5f * x * y * y);
}

int main() {
    std::clock_t start;// = std::clock();
    std::clock_t end;
    float rootMe;

    int iterations = 999999999;

    // ---

    rootMe = 2.0f;
    start = std::clock();

    std::cout << "Math.h SqRoot: ";

    for (int m = 0; m < iterations; m++) {
        (float)(1.0/sqrt(rootMe));
        rootMe++;
    }

    end = std::clock();

    std::cout << (difftime(end, start)) << std::endl;

    // ---

    std::cout << "Quake SqRoot: ";

    rootMe = 2.0f;
    start = std::clock();

    for (int q = 0; q < iterations; q++) {
        invSqrt(rootMe);
        rootMe++;
    }

    end = std::clock();

    std::cout << (difftime(end, start)) << std::endl;   
}
+3
source share
1 answer

There are several problems in the tests. First, your benchmark includes a potentially expensive listing from int to float. If you want to know what the square root is, you should compare the square roots, not the data transforms.

-, ( ), . ( ), , .

, . , . . , , , . , , , , , .

, , . , , 1.0/... Quake. ( - , )

, , , Carmacks 12- . , , , , , "" .

+5

Source: https://habr.com/ru/post/1766536/


All Articles