Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast Sqrt 2
(version: 0)
Compare Quakes fast inverse squareroot
Comparing performance of:
Math.sqrt vs Quakes fast inverse sqrt.
Created:
5 years ago
by:
Registered User
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 const bytes = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT); const floatView = new Float32Array(bytes); const intView = new Uint32Array(bytes); const threehalfs = 1.5; function Q_rsqrt(number) { const x2 = number * 0.5; floatView[0] = number; intView[0] = 0x5f3759df - (intView[0] >> 1); let y = floatView[0]; y = y * (threehalfs - (x2 * y * y)); 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
761630.8 Ops/sec
Quakes fast inverse sqrt.
141052.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition JSON** The provided Benchmark Definition JSON represents a comparison between two test cases: `test1()` and `test2()`, which measure the performance of calculating the square root of 2 using the built-in `Math.sqrt()` function and the Quake's fast inverse square root algorithm, respectively. **Options Compared** Two options are compared: 1. **`Math.sqrt()`**: This is the built-in JavaScript method for calculating the square root of a number. 2. **Quake's fast inverse square root algorithm (`Q_rsqrt()`)**: This is a custom implementation of the inverse square root function, designed to be faster than the built-in `Math.sqrt()` method. **Pros and Cons** Here are some pros and cons of each approach: * **`Math.sqrt()`**: + Pros: Wide adoption, good performance in most cases, easy to implement. + Cons: May not be optimized for specific use cases or hardware. * **Quake's fast inverse square root algorithm (`Q_rsqrt()`)**: + Pros: Optimized for certain hardware and architectures, can provide significant performance improvements in specific use cases. + Cons: Requires a custom implementation, may have a higher overhead due to the additional code. The Quake's fast inverse square root algorithm is designed to take advantage of the IEEE 754 floating-point representation and uses bitwise operations to calculate the inverse square root. While it provides better performance in certain scenarios, it also introduces additional complexity and may not be suitable for all use cases. **Library: `Float32Array`** The Quake's fast inverse square root algorithm uses the `Float32Array` library to manipulate floating-point numbers. This library is part of the JavaScript standard library and provides an efficient way to work with 32-bit floating-point numbers. **Special JS Feature/Syntax** None are explicitly mentioned in the provided Benchmark Definition JSON, but it's worth noting that JavaScript has several features and syntax that can impact performance, such as: * **Just-In-Time (JIT) compilation**: Modern JavaScript engines like V8 (used by Chrome) and SpiderMonkey (used by Firefox) use JIT compilation to improve performance. * **Cache-friendly memory access patterns**: The Quake's fast inverse square root algorithm is designed to minimize cache misses, which can significantly impact performance. **Other Alternatives** If you're looking for alternative implementations of the square root function or want to explore other optimization techniques, here are a few options: * **Google's V8 JavaScript Engine**: The V8 engine provides several optimization techniques, including JIT compilation and cache-friendly memory access patterns. * **SpiderMonkey JavaScript Engine**: SpiderMonkey also provides various optimization techniques, including JIT compilation and custom inline caching. * **Native libraries**: Depending on the platform and use case, native libraries like `libm` (on Linux) or `libc` (on Windows) can provide optimized implementations of mathematical functions, including square root calculations.
Related benchmarks:
Fast Sqrt
Fast Sqrt 2234234
Inv Fast Sqrt
Fast Sqrt0
Comments
Confirm delete:
Do you really want to delete benchmark?