Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Inverse square root (fixed)
(version: 0)
Comparing performance of:
Traditional square root vs Fast square root
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let fArr = new Float32Array(1); let iArr = new Int32Array(fArr.buffer); function fsr(n) { fArr[0] = n; iArr[0] = 0x5f375a86 - ( iArr[0] >> 1 ); fArr[0] = fArr[0] * (1.5 - (n * 0.5) * fArr[0] * fArr[0]); return fArr[0]; }
Tests:
Traditional square root
let sqroots = []; for (let i = 0; i < 5000; i++) { sqroots.push(Math.sqrt(Math.random()*i)); }
Fast square root
let sqroots = []; for (let i = 0; i < 5000; i++) { sqroots.push(fsr(Math.random()*i)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Traditional square root
Fast square root
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance of two approaches for calculating the square root: the traditional method using `Math.sqrt()` and a custom implementation using the inverse square root algorithm. **Benchmark Definition JSON** The first section of the benchmark definition JSON represents the preparation code for the inverse square root algorithm. It defines: 1. Two arrays, `fArr` and `iArr`, where `fArr` is a Float32Array and `iArr` is an Int32Array. 2. A function `fsr(n)` that takes a single argument `n`. This function calculates the square root of `n` using the inverse square root algorithm. The inverse square root algorithm is a fast method for calculating square roots, which was popularized by John Carmack (id Software's co-founder) in the 1990s. It uses a combination of bit manipulation and polynomial approximation to achieve high performance. **Options Compared** Two options are compared: 1. **Traditional Square Root**: Uses the built-in `Math.sqrt()` function. 2. **Fast Square Root**: Implements the inverse square root algorithm as defined in the benchmark definition JSON. **Pros and Cons of Each Approach** **Traditional Square Root (using Math.sqrt())** Pros: * Easy to understand and implement * Wide support across browsers and platforms * Fast for small inputs Cons: * Slow for large inputs due to the overhead of the built-in function call * May not be optimized for performance in every browser or platform **Fast Square Root (inverse square root algorithm)** Pros: * Fast for both small and large inputs * Optimized for performance in many browsers and platforms Cons: * More complex to understand and implement, especially for those without prior experience with bit manipulation and polynomial approximation. * May not be supported by all browsers or platforms. **Library and Syntax** In the individual test cases, two libraries are used: 1. `Math` library (not explicitly stated, but implied by the use of `Math.sqrt()` and other functions) 2. None (the custom implementation of the inverse square root algorithm) No special JavaScript features or syntax are mentioned in the benchmark. **Alternative Approaches** Other alternative approaches for calculating square roots include: * **Brent's Method**: A more efficient version of the inverse square root algorithm, which uses a combination of bit manipulation and polynomial approximation. * **Newton's Method**: An iterative method that refines an initial guess for the square root. While slower than the inverse square root algorithm, it is more widely supported and can be used for small inputs where accuracy is not critical. It's worth noting that modern JavaScript engines, such as V8 (used by Chrome), have optimized implementations of `Math.sqrt()` that use a combination of branch prediction, loop unrolling, and other techniques to achieve high performance.
Related benchmarks:
Inverse square root
Fast Sqrt
Native
Fast Sqrt 2234234
Comments
Confirm delete:
Do you really want to delete benchmark?