Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast Sqrt0
(version: 0)
Compare Quakes fast inverse squareroot
Comparing performance of:
Math.sqrt vs Quakes fast inverse sqrt.
Created:
3 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 < 100000; ++i) { r = Math.sqrt(v); } } function test2() { for (i = 0; i < 100000; ++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:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 OPR/119.0.0.0
Browser/OS:
Opera 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.sqrt
6056.5 Ops/sec
Quakes fast inverse sqrt.
40.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition JSON** The provided Benchmark Definition JSON represents a comparison between two functions: `Math.sqrt` and a custom implementation of the fast inverse square root function (`Q_rsqrt`). The `Fast Sqrt0` benchmark is designed to measure the performance difference between these two approaches. **What is being tested?** The test cases are comparing the execution speed of: 1. **`Math.sqrt`**: A built-in JavaScript function that calculates the square root of a given number. 2. **`Q_rsqrt`**: A custom implementation of the fast inverse square root function, which is based on an optimized algorithm from Wikipedia. **Options being compared** The two options being compared are: 1. `Math.sqrt`: The built-in JavaScript function. 2. `Q_rsqrt`: The custom implementation of the fast inverse square root function. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`Math.sqrt`**: + Pros: Easy to use, well-documented, and widely supported. + Cons: May be slower than optimized implementations like `Q_rsqrt`. * **`Q_rsqrt`**: + Pros: Optimized for performance, can be faster than built-in functions. + Cons: Requires manual implementation of the fast inverse square root algorithm, which can be complex. **Library and purpose** The `Q_rsqrt` function uses a library-like approach by: 1. Creating a new `ArrayBuffer` to store the input value. 2. Converting the input value to an integer using `new Uint32Array(buf)[0]`. 3. Applying the optimized algorithm to calculate the square root. This implementation is similar to how the fast inverse square root function works, but with a JavaScript twist. **Special JS feature or syntax** No special features or syntax are used in this benchmark. **Other alternatives** If you were to implement your own `Q_rsqrt` function without using an array buffer-like approach, you could use: 1. SIMD (Single Instruction, Multiple Data) instructions: Some modern CPUs support SIMD instructions that can perform operations on multiple values simultaneously. 2. Approximate calculations: You could use approximate methods like Newton's method or binary search to estimate the square root. However, these alternatives might not be as efficient as using an optimized library-like approach or a built-in function like `Math.sqrt`. Keep in mind that this benchmark is designed to compare the performance of two specific functions, and the results may vary depending on your specific use case and JavaScript environment.
Related benchmarks:
Fast Sqrt
Fast Sqrt 2
Fast Sqrt 2234234
Inv Fast Sqrt
Comments
Confirm delete:
Do you really want to delete benchmark?