Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Inv Fast Sqrt
(version: 0)
Compare Quakes fast inverse squareroot with reciprocal of math squareroot
Comparing performance of:
Reciprocal Math.sqrt vs Quakes fast inverse sqrt.
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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; }
Tests:
Reciprocal Math.sqrt
test1();
Quakes fast inverse sqrt.
test2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reciprocal Math.sqrt
Quakes fast inverse sqrt.
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reciprocal Math.sqrt
85140.0 Ops/sec
Quakes fast inverse sqrt.
971.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Reciprocal Math.sqrt**: This test case uses the built-in `Math.sqrt` function in JavaScript, which is a standard library function that returns the square root of a given number. 2. **Quakes fast inverse sqrt.**: This test case uses a custom implementation of the inverse square root function, called "Q_rsqrt," which is based on a proprietary algorithm described in the Wikipedia article on Fast Inverse Square Root. **Comparison** The two tests compare the performance of these two implementations: * The first test case ("Reciprocal Math.sqrt") measures the execution time of the built-in `Math.sqrt` function. * The second test case ("Quakes fast inverse sqrt.") measures the execution time of the custom "Q_rsqrt" implementation. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Reciprocal Math.sqrt**: Pros: * Standard library function, widely supported across browsers and platforms. * Easy to implement and maintain. Cons: * May have slower performance compared to custom implementations, especially for large inputs. 2. **Quakes fast inverse sqrt.**: * Custom implementation that can potentially outperform the standard library function for specific use cases (e.g., games or applications requiring low latency). Pros: Can be optimized for specific use cases and may provide better performance for certain inputs. 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: 1. **Performance**: If low latency is critical for your application, you may want to opt for the custom "Q_rsqrt" implementation. However, if simplicity and wide compatibility are more important, the built-in `Math.sqrt` function might be a better choice. 2. **Code Complexity**: The custom "Q_rsqrt" implementation is more complex compared to the standard library function. This can lead to higher maintenance costs or difficulties in debugging and optimizing. 3. **Platform Support**: Be aware that not all browsers or platforms may support the custom "Q_rsqrt" implementation, which could impact compatibility. **Alternatives** If you're interested in exploring alternative implementations for the inverse square root function, consider the following: 1. **Other proprietary algorithms**: There are other proprietary algorithms available for calculating the inverse square root, such as the one described in the Wikipedia article on Fast Inverse Square Root. However, these may not be as widely supported or optimized as the custom "Q_rsqrt" implementation. 2. **Open-source implementations**: You can explore open-source implementations of the inverse square root function, such as those available in libraries like GLMath or MathJunkie. 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.
Related benchmarks:
Fast Sqrt
Fast Sqrt 2
Fast Sqrt 2234234
Fast Sqrt0
Comments
Confirm delete:
Do you really want to delete benchmark?