Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Bool generation from uint32
(version: 1)
Comparing performance of:
Compare to half of max int vs Mod even/odd vs Bitwise even/odd
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function getRandomUint32(min, max) { return Math.floor(Math.random() * (max - min) + min); }
Tests:
Compare to half of max int
for(let i = 0; i < 100; i++) { const z = getRandomUint32(0, 2 ** 32) < 2_147_483_648; }
Mod even/odd
for(let i = 0; i < 100; i++) { const z = getRandomUint32(0, 2 ** 32) % 2 === 0; }
Bitwise even/odd
for(let i = 0; i < 100; i++) { const z = getRandomUint32(0, 2 ** 32) & 1 === 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Compare to half of max int
Mod even/odd
Bitwise even/odd
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Compare to half of max int
21006774.0 Ops/sec
Mod even/odd
20871864.0 Ops/sec
Bitwise even/odd
20994424.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that tests the performance of different approaches for generating boolean values from 32-bit unsigned integers. The benchmark consists of three test cases: 1. Comparing a generated value to half of the maximum integer. 2. Checking if a generated value is even (remainder when divided by 2 is 0). 3. Using bitwise operations to check if a generated value is odd (least significant bit is 1). **Benchmark Preparation Code** The `getRandomUint32` function is used to generate random 32-bit unsigned integers. This function uses the `Math.random()` method to generate a random number, multiplies it by `(max - min) + 1`, and then adds `min` to shift the range. **Options Compared** In each test case, the options being compared are: * A direct comparison using `<` * A modulo operation with `%` * A bitwise AND operation with `&` These options differ in their implementation and potential performance implications. **Pros and Cons of Each Approach** 1. **Direct Comparison (`<`)** * Pros: Simple and straightforward, easy to understand. * Cons: May result in floating-point arithmetic or integer overflows for very large values. 2. **Modulo Operation (`%` )** * Pros: Efficient for even/odd checks, reduces potential for overflows. * Cons: Requires modulo operation, which may be slower than direct comparison. 3. **Bitwise AND (`&`)** * Pros: Fast and efficient, minimizes the number of operations required to check if a value is odd. * Cons: May require additional instructions (e.g., bitwise shift) to convert to an even/odd flag. **Library and Special JS Features** There are no libraries or special JavaScript features used in this benchmark. The `getRandomUint32` function is a simple utility function that can be easily implemented by any JavaScript engine. **Device and Browser Considerations** The benchmark results show that Firefox 128 on Windows Desktop outperforms the other options. This may be due to optimizations or features specific to Firefox, such as Just-In-Time (JIT) compilation or hardware acceleration. **Other Alternatives** For generating boolean values from integers, you can also consider using: * Bitwise shift operators (`>>` for right shift and `<<` for left shift) in combination with the least significant bit. * Modular arithmetic operations other than `%`, such as using the exponentiation operator (`**`) or the modulo operator (`%`). * Using a library like NumJS or Bigint.js to handle large integers. Keep in mind that these alternatives may have different performance characteristics and trade-offs, depending on the specific use case and requirements.
Related benchmarks:
Random Integer Generator (favors numbers closer to 0) 2
Fisher-Yates Shuffle
getRandomNumberInRange vs getRandomValueInRange
getRandomNumberInRange vs getRandomValueInRange 5000
Comments
Confirm delete:
Do you really want to delete benchmark?