Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
mod / &
(version: 1)
Comparing performance of:
mod vs &
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const MAX = 0x7fff let i = 0;
Tests:
mod
while (i < MAX) { i = (i % MAX) + 1; }
&
while (i < MAX) { i = (i & MAX) + 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
mod
&
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
mod
148050256.0 Ops/sec
&
142740464.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark represented in the provided JSON tests two different operations within a loop: using the modulus operator (`%`) and the bitwise AND operator (`&`). Each operation is performed in a while loop that iterates until a counter variable, `i`, exceeds a predefined maximum value (`MAX`, which is set to `0x7fff`, or 32767 in decimal). ### Benchmark Breakdown 1. **Operations Tested**: - **Modulus Operation (`%`)**: In the first test case named "mod", the operation inside the loop is `i = (i % MAX) + 1;`. The modulus operator computes the remainder of division of `i` by `MAX`, effectively wrapping `i` around to stay within the range of `1` to `MAX`. - **Bitwise AND Operation (`&`)**: In the second test named "&", the operation is `i = (i & MAX) + 1;`. The bitwise AND operator compares each bit of `i` with the corresponding bit in `MAX`. As `MAX` is `0x7fff` (or `32767`), which in binary is `0111 1111 1111 1111`, this operation will constrain `i` to the same maximum value, but it doesn't "wrap" it like the modulus operation. ### Pros and Cons - **Modulus Operator (`%`)**: - **Pros**: - It is straightforward for wrapping values within a specific range (0 to MAX). - It works well with general numeric operations that require a modulo relationship. - **Cons**: - The modulus operation can be slower than a simple bitwise operation because it involves division, which is generally more computationally expensive than bitwise manipulation. - **Bitwise AND Operator (`&`)**: - **Pros**: - Bitwise operations are typically faster than arithmetic operations like division, making the bitwise AND a more efficient choice in scenarios well-suited for bit-level manipulation. - It can be used for "masking" specific bits, useful in low-level programming or optimizations. - **Cons**: - It is less intuitive than the modulus operation for cases where value wrapping is desired, as it does not inherently wrap around when the value exceeds the maximum unless handled explicitly. ### Alternative Considerations In the context of these two operations, using bitwise vs. modulus operations can depend on the specific requirements of the application. If you anticipate needing to regularly constrain values within a specific range, the modulus operator is preferable for clarity. However, if performance is crucial and the target values fall within a specific power of two, bitwise operations may be advantageous. ### Other Alternatives There are other options to consider when determining how to handle value wrapping or constraints: 1. **Conditional Checks**: Instead of using either operator, one could check if `i` exceeds `MAX` and reset it conditionally. This can lead to increased overhead in performance due to branching. 2. **Mathematical Operations**: For scenarios outside of simply wrapping values, mathematical transformations or other arrangements of operations might provide an advantage depending on the specific needs of the computation. Overall, the benchmark demonstrates a direct performance comparison between two different programming strategies for modifying the same variable, each having its own use cases and implications for performance.
Related benchmarks:
Anonymous Function in Loop
test of whatever
assignment
!== vs ===
JS Loop
weefwfe
if and direct call
Anonymous Function in Loop 2
xxxxxxxxxxxxx
Comments
Confirm delete:
Do you really want to delete benchmark?