Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast Sqrt
(version: 0)
Compare Quakes fast inverse squareroot
Comparing performance of:
Math.sqrt vs Quakes fast inverse sqrt.
Created:
7 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 = Math.sqrt(v); } } function test2() { for(i=0; i < 1000; ++i) { r = Q_rsqrt(v) * 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:
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
Math.sqrt
Quakes fast inverse sqrt.
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.sqrt
45726.2 Ops/sec
Quakes fast inverse sqrt.
494.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is testing two JavaScript functions: `test1()` and `test2()`. The latter uses the Quakes fast inverse square root function, which is a custom implementation of the fast inverse square root algorithm. This algorithm is an optimization for calculating the square root in floating-point arithmetic, particularly useful for high-performance applications. **Functionality of test1()** `test1()` calculates the square root of a large number (`v`) using the built-in `Math.sqrt()` function 1000 times. The purpose of this test is to evaluate how fast the `Math.sqrt()` function executes. **Functionality of test2()** `test2()` also calculates the square root of `v`, but it uses the Quakes fast inverse square root function, which is a custom implementation that attempts to mimic the original fast inverse square root algorithm. This test aims to compare the performance of this custom implementation with the built-in `Math.sqrt()` function. **Comparison** The benchmark compares the execution time of two approaches: 1. **Built-in Math.sqrt()**: The first test case (`test1()`) measures the execution time of using the built-in `Math.sqrt()` function. 2. **Quakes fast inverse sqrt.**: The second test case (`test2()`) measures the execution time of using the custom Quakes fast inverse square root function. **Pros and Cons** Pros: * The use of a benchmarking framework allows for a fair comparison between different implementations. * The tests cover two common scenarios: using built-in functions versus custom optimizations. Cons: * Using a custom implementation (Quakes fast inverse sqrt.) may introduce additional complexity and potential errors, which could affect the results. * The Quakes fast inverse square root function is not part of the standard JavaScript library, so it might not be supported by all browsers or environments. **Library: FastInverseSqrt** The `FastInverseSqrt` library is a custom implementation of the fast inverse square root algorithm. It's likely that this library is designed for high-performance applications and provides an optimized solution for calculating square roots in floating-point arithmetic. **Other alternatives** In general, when it comes to calculating square roots in JavaScript: 1. **Built-in Math.sqrt()**: This is a reliable and widely supported function that works well for most use cases. 2. **Custom implementations**: Optimized custom implementations like Quakes fast inverse sqrt. might be useful in specific high-performance applications where the built-in function is not sufficient. However, it's essential to note that using custom implementations can add complexity and potential errors, so they should be used judiciously. **Special JS feature or syntax** This benchmark does not use any special JavaScript features or syntax beyond what's required for the implementation of the Quakes fast inverse square root function.
Related benchmarks:
Fast Sqrt 2
Fast Sqrt 2234234
Inv Fast Sqrt
Fast Sqrt0
Comments
Confirm delete:
Do you really want to delete benchmark?