Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Selection Algorithm Comparison 2
(version: 0)
Comparing performance of:
Algorithm 1 vs Algorithm 2
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function gaussian(mean, variance) { return Math.sqrt(-2 * variance * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random()) + mean; }
Tests:
Algorithm 1
const n = 2; const columns = 4*n; const rows = 3*n; const cellWidth = 640/columns; const matrix = {}; let population = rows*columns; const numParents = Math.ceil(0.2*rows*columns); for (let i = 0; i < rows; i++) { matrix[i] = {}; for (let j = 0; j < columns; j++) { matrix[i][j] = 0; } } let p = 0; while (p < numParents) { try { for (let i = 0; i < rows; i++) { for (let j = 0; j < columns; j++) { if (p < numParents) { if (matrix[i][j] === 1) { continue; } if (Math.abs(gaussian(0,columns) > j)) { matrix[i][j] = 1; ++p; } } else { throw ""; } } } } catch (e) {} }
Algorithm 2
const n = 2; const columns = 4*n; const rows = 3*n; const cellWidth = 640/columns; const matrix = {}; for (let i = 0; i < rows; i++) { matrix[i] = {} for (let j = 0; j < columns; j++) { matrix[i][j] = 0 } } let population = rows*columns; let numParents = Math.ceil(0.2*rows*columns); for (let i = 0; i < numParents; i++) { let success = false; while (!success) { let columnIndex = null; while (columnIndex === null) { const rand = Math.floor(Math.abs(gaussian(0, columns))); console.log(rand); if (rand < columns) { columnIndex = rand } } const rowIndex = Math.floor(Math.random()*rows); if (matrix[rowIndex][columnIndex] === 0) { matrix[rowIndex][columnIndex] = 1; success = true; } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Algorithm 1
Algorithm 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares two selection algorithms for generating random numbers. **Benchmark Definition JSON Analysis** The provided Benchmark Definition JSON defines two test cases, "Algorithm 1" and "Algorithm 2". Both tests use a simulated population of cells in a matrix, where each cell is initialized to 0. A parent cell is randomly selected, and with a probability of 20% (based on the Gaussian distribution), the cell is set to 1. **Options Compared** The two algorithms being compared are: 1. **Algorithm 1**: This algorithm generates random column indices using `Math.random()` and selects a row index based on that value. 2. **Algorithm 2**: This algorithm uses a while loop to generate a random number between 0 and the total number of columns, effectively creating a uniform distribution. **Pros and Cons** * **Algorithm 1**: + Pros: Simple and efficient implementation. + Cons: May not produce a truly uniform distribution due to the limitations of `Math.random()`. * **Algorithm 2**: + Pros: Produces a more accurate uniform distribution by generating random numbers within the range of the total number of columns. + Cons: More complex implementation, potentially slower. **Library and Purpose** The Gaussian function used in both algorithms is a common mathematical formula for generating random numbers from a normal distribution. In this context, it's used to simulate the natural variability of real-world data. **Special JavaScript Feature/ Syntax** None mentioned in the provided Benchmark Definition JSON. **Other Considerations** When selecting a random number generator, it's essential to consider factors such as: * **Uniformity**: How evenly is the distribution of generated numbers? * **Speed**: How quickly can the algorithm generate random numbers? * **Consistency**: Does the algorithm produce consistent results across multiple runs? **Alternatives** If you're looking for alternative JavaScript libraries or implementations, some popular options include: * **Crypto-Random**: A built-in JavaScript API for generating cryptographically secure random numbers. * **Random.org**: A widely used online random number generator that provides a uniform distribution. Keep in mind that the choice of library or implementation ultimately depends on your specific use case and requirements.
Related benchmarks:
Sort Comparator IF statements vs OR (||) assignment
Sorting algorithms comparison (source: https://www.measurethat.net/Benchmarks/Show/3549/0/javascript
quickselect_median
Javascript Sorting Algorithms mdhe
Javascript Sorting Algorithms mdhe3
Comments
Confirm delete:
Do you really want to delete benchmark?