function gaussian(mean, variance) {
return Math.sqrt(-2 * variance * Math.log(Math.random())) *
Math.cos(2 * Math.PI * Math.random()) + mean;
}
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) {}
}
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;
}
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Algorithm 1 | |
Algorithm 2 |
Test name | Executions per second |
---|---|
Algorithm 1 | 19.1 Ops/sec |
Algorithm 2 | 67.9 Ops/sec |
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:
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.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
Other Considerations
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:
"const n = 20; const columns = 4*n;"
)Math.floor(Math.abs(gaussian(0, columns)))
)Alternatives
There are several alternatives to the benchmarked algorithms, including:
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.