Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Bitwise vs modulo (2)
(version: 0)
Comparing performance of:
Modulo vs Bitwise
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var num = Math.trunc(Math.random()*10);
Tests:
Modulo
num % 2
Bitwise
num ^ 1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Modulo
Bitwise
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Modulo
1023543552.0 Ops/sec
Bitwise
988841472.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark configuration and explanation. **Benchmark Overview** The provided benchmark measures the performance of two different approaches to check if a number is even: using the modulo operator (`%`) and bitwise operations (`^`). The benchmark is run in Chrome 120 on a Windows desktop. **Script Preparation Code** The script preparation code initializes a variable `num` with a random integer value between 0 and 9, generated using `Math.trunc(Math.random()*10)`. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only runs in a Node.js environment (since MeasureThat.net is a Node.js-based tool). **Benchmark Definition JSON** The benchmark definition consists of two individual test cases: 1. **Modulo**: This test case uses the modulo operator (`%`) to check if `num` is even. The `Benchmark Definition` string is simply "num % 2", which means that the code will calculate the remainder of `num` divided by 2. 2. **Bitwise**: This test case uses bitwise operations (`^`) to check if `num` is odd. The `Benchmark Definition` string is "num ^ 1", which means that the code will perform a bitwise XOR operation between `num` and 1. **Libraries** There are no libraries used in this benchmark, so there's nothing to discuss. **Special JS Features/Syntax** The benchmark uses two special JavaScript features: * **Bitwise Operations**: The `^` operator is used for bitwise XOR, which performs a binary operation on the bits of its operands. * **Random Number Generation**: The `Math.random()` function generates a random number between 0 (inclusive) and 1 (exclusive). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Modulo Operator (`%`)**: + Pros: Easy to understand, widely supported, and efficient. + Cons: May be slower than bitwise operations for very large numbers or specific edge cases. * **Bitwise Operations (`^`)**: + Pros: Can be faster for certain cases (e.g., 64-bit integers) due to lower CPU overhead. Also can provide more control over the operation. + Cons: Less intuitive, may require more code to implement correctly. **Other Alternatives** If you want to measure the performance of other approaches to check if a number is even, here are some alternatives: * Using the `&` operator with the bitwise AND mask 1 (`num & 1 == 0`) for an even simpler implementation. * Using a custom function with conditional logic (e.g., `if (num % 2 === 0) { ... }`) which may be more intuitive but potentially slower. Keep in mind that these alternatives might not provide the same level of optimization as the bitwise XOR approach, and it's essential to profile and test your specific use cases before deciding on an implementation.
Related benchmarks:
Math.floor vs bitwise <<
Truncating a number to an integer
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
floor vs trunc vs bit shift
Comments
Confirm delete:
Do you really want to delete benchmark?