Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Selection Algorithm Comparison
(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 = 20; 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 = 20; 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):
I'll explain the benchmark in detail. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares two selection algorithms: Algorithm 1 and Algorithm 2. The benchmark is designed to test the performance of these algorithms on a population of cells, where each cell has a probability of being selected based on a Gaussian distribution. **Script Preparation Code** The Script Preparation Code is a function named `gaussian` that calculates the inverse of a Gaussian distribution. This function is used in both Algorithm 1 and Algorithm 2 to determine the probability of selecting a cell. **Html Preparation Code** There is no Html Preparation Code provided, which means that the benchmark does not require any HTML setup or rendering. **Individual Test Cases** The two test cases are: 1. **Algorithm 1**: This algorithm iterates through each cell in the population and attempts to select it using the `gaussian` function. If the cell has already been selected (i.e., its value is 1), it continues to the next cell. Otherwise, it calculates the probability of selecting the cell based on the Gaussian distribution and updates the cell's value if the condition is met. 2. **Algorithm 2**: This algorithm also iterates through each cell in the population and attempts to select it using a while loop. It generates a random column index using the `gaussian` function and selects the cell at that index if it has not already been selected (i.e., its value is 0). **Comparison of Algorithms** The benchmark compares the performance of Algorithm 1 and Algorithm 2 on the population of cells. The algorithm with the higher number of executions per second is considered to be more efficient. **Pros and Cons of Each Approach** * **Algorithm 1**: This algorithm uses a nested loop structure, which can lead to slower performance due to the increased number of iterations. * **Algorithm 2**: This algorithm uses a while loop and randomization, which can introduce additional overhead due to the uncertainty in selecting cells. **Other Considerations** * The benchmark assumes that the population of cells is evenly distributed and that the Gaussian distribution is symmetric. * The benchmark does not account for any potential biases or imbalances in the selection process. **Library Usage** There is no explicit library usage in this benchmark, but it uses a standard JavaScript `Math` library for the `gaussian` function calculation. **Special JS Features or Syntax** The benchmark uses some advanced JavaScript features, such as: * The use of template literals (e.g., `"const n = 20; const columns = 4*n;"`) * The use of arrow functions (not explicitly shown in the code snippet) * The use of regular expressions (`Math.floor(Math.abs(gaussian(0, columns)))`) **Alternatives** There are several alternatives to the benchmarked algorithms, including: * Other selection algorithms, such as tournament selection or roulette wheel selection. * Different data structures for representing the population, such as arrays or objects. * Optimizations to reduce the overhead of randomization, such as using a hash table or a cache. Overall, this benchmark provides a useful comparison between two selection algorithms and highlights some of the trade-offs involved in choosing an algorithm for a specific problem.
Related benchmarks:
Javascript Sorting Algorithms
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?