Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
math.abs vs branchless absolute val 2
(version: 0)
Comparing performance of:
branchless vs native vs actual branchless
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var abs = Math.abs, x = -2131.1283712;
Tests:
branchless
return (x < 0) * -x + (x >= 0) * x;
native
return abs(x);
actual branchless
return x & -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
branchless
native
actual branchless
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
branchless
147950080.0 Ops/sec
native
148764880.0 Ops/sec
actual branchless
148762384.0 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** The benchmark definition provided is a simple mathematical expression that tests three different approaches to calculate the absolute value of a number: 1. **Branchless**: `return (x < 0) * -x + (x >= 0) * x;` 2. **Native** (using the built-in `Math.abs` function): `return abs(x);` 3. **Actual Branchless**: `return x & -1;` Here's a brief explanation of each approach: * **Branchless**: This method uses bitwise operations to determine whether the number is negative or non-negative without using conditional branches (if/else statements). * **Native** (using `Math.abs`): This method simply calls the built-in `Math.abs` function, which performs the calculation directly. * **Actual Branchless**: This method also uses a branchless approach, but with a different combination of bitwise operations. **Pros and Cons** Here's a brief summary of each approach: * **Branchless**: + Pros: Can potentially be faster since it avoids branches. + Cons: May require more complex calculations and bitwise operations, which can increase the chance of errors. * **Native (Math.abs)**: + Pros: Simple and reliable; uses built-in function for accurate results. + Cons: May not be optimized for performance, as it's a straightforward call to `Math.abs`. * **Actual Branchless**: + Pros: Similar to branchless approach but with different bitwise operations; might offer similar performance benefits. + Cons: Less intuitive and potentially more error-prone due to the specific combination of bitwise operations. **Library: None** There are no external libraries used in this benchmark definition. **Special JavaScript Feature/Syntax: None** The benchmark does not use any special JavaScript features or syntax, making it accessible to a wide range of software engineers. **Other Alternatives** If you're interested in exploring alternative approaches, here are some additional methods you could consider: * **Truncating with two's complement**: `return (x < 0) ? -x : x;` (similar to branchless approach but using two's complement) * **Bitwise shift and comparison**: `return ((~x & 1) == 0) ? x : -x;` (another branchless approach using bitwise operations) Keep in mind that these alternatives might have different performance characteristics, so it's essential to test them thoroughly to determine their suitability for your specific use case. **Benchmark Preparation Code and Test Cases** The provided benchmark preparation code is: ```javascript var abs = Math.abs, x = -2131.1283712; ``` And the individual test cases are: * `branchless` * `native` * `actual branchless` These test cases exercise each of the three approaches to calculate the absolute value. **Latest Benchmark Result** The provided benchmark result shows the performance of each approach on a Chrome 110 browser on a Windows desktop: | Test Name | Executions Per Second | | --- | --- | | actual branchless | 15546077.0 | | native | 8124468.5 | | branchless | 4062586.5 | These results indicate that the `actual branchless` approach is the fastest, followed closely by the `native` approach, and then the `branchless` approach. I hope this explanation helps you understand the benchmark definition and the different approaches used to calculate the absolute value of a number!
Related benchmarks:
Truncating a number to an integer
math.abs vs branchless absolute val
Math.abs speed vs multiply
Math.abs vs binary Math.abs
Comments
Confirm delete:
Do you really want to delete benchmark?