var i;
var r;
var v = 2**16;
function test1() {
for(i=0; i < 1000; ++i) {
r = 1.0/Math.sqrt(v);
}
}
function test2() {
for(i=0; i < 1000; ++i) {
r = Q_rsqrt(v);
}
}
//Based on the fast inverse square root function
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
// Some original comments preserved for humor value
// Designed to try to mimic the original as closely as possible
function Q_rsqrt(number)
{
var i;
var x2, y;
const threehalfs = 1.5;
x2 = number * 0.5;
y = number;
//evil floating bit level hacking
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = number;
i = (new Uint32Array(buf))[0];
i = (0x5f3759df - (i >> 1)); //What the fuck?
(new Uint32Array(buf))[0] = i;
y = (new Float32Array(buf))[0];
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
test1();
test2();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Reciprocal Math.sqrt | |
Quakes fast inverse sqrt. |
Test name | Executions per second |
---|---|
Reciprocal Math.sqrt | 7757.2 Ops/sec |
Quakes fast inverse sqrt. | 1539.3 Ops/sec |
Let's break down the provided JSON and explain what is tested on it, along with the pros and cons of different approaches.
Benchmark Definition
The provided JSON defines two test cases: "Reciprocal Math.sqrt" and "Quakes fast inverse sqrt." These tests are designed to compare the performance of two different implementations of the inverse square root function:
Math.sqrt
function in JavaScript, which is a standard library function that returns the square root of a given number.Comparison
The two tests compare the performance of these two implementations:
Math.sqrt
function.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Cons: * May not be as widely supported across browsers and platforms, especially if the custom implementation is not properly implemented. * Requires more expertise to implement and maintain compared to standard library functions.
Other Considerations
When evaluating these two approaches, consider the following:
Math.sqrt
function might be a better choice.Alternatives
If you're interested in exploring alternative implementations for the inverse square root function, consider the following:
Keep in mind that implementing a custom inverse square root function requires expertise and may not provide significant performance benefits for general-purpose JavaScript applications.