Skip to content

Commit

Permalink
benchmark: add quicksort benchmark of float elem (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: zhenweijin <[email protected]>
  • Loading branch information
kylo5aby authored Dec 18, 2023
1 parent 6ace1f1 commit e130b99
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/benchmark/quicksort_float.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* This file is generated by tsc */
"use strict";
/* This file is modified base on:
* https://browserbench.org/JetStream/wasm/quicksort.c
*/
var SORTELEMENTS = 1e5;
var seed = 0;
var maximum, minimum;
var sortList = new Array(SORTELEMENTS + 1);
function rand() {
seed = (seed * 1309 + 13849) & 65535;
return seed;
}
function initArr() {
seed = 74755;
maximum = 0;
minimum = 65536;
for (var i = 1; i <= SORTELEMENTS; i++) {
const val = rand();
sortList[i] = val + val / 65536;
if (sortList[i] > maximum) {
maximum = sortList[i];
}
else if (sortList[i] < minimum) {
minimum = sortList[i];
}
}
}
function quickSort(a, l, r) {
var i = l, j = r;
var w;
var idx = Math.floor((l + r) / 2);
var x = a[idx];
do {
while (a[i] < x)
i++;
while (x < a[j])
j--;
if (i <= j) {
w = a[i];
a[i] = a[j];
a[j] = w;
i++;
j--;
}
} while (i <= j);
if (l < j)
quickSort(a, l, j);
if (i < r)
quickSort(a, i, r);
}
function main() {
initArr();
quickSort(sortList, 1, SORTELEMENTS);
if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) {
console.log('Validate result error in quicksort');
}
}

main();
58 changes: 58 additions & 0 deletions tests/benchmark/quicksort_float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* This file is modified base on:
* https://browserbench.org/JetStream/wasm/quicksort.c
*/
const SORTELEMENTS = 1e5;
let seed = 0;
let maximum: number, minimum: number;
const sortList = new Array<number>(SORTELEMENTS + 1);

function rand() {
seed = (seed * 1309 + 13849) & 65535;
return seed;
}

function initArr() {
seed = 74755;
maximum = 0;
minimum = 65536;

for (let i = 1; i <= SORTELEMENTS; i++) {
const val = rand();
sortList[i] = val + val / 65536;
if (sortList[i] > maximum) {
maximum = sortList[i];
} else if (sortList[i] < minimum) {
minimum = sortList[i];
}
}
}

function quickSort(a: number[], l: number, r: number) {
let i = l,
j = r;
let w: number;
const idx = Math.floor((l + r) / 2);
const x = a[idx];
do {
while (a[i] < x) i++;
while (x < a[j]) j--;
if (i <= j) {
w = a[i];
a[i] = a[j];
a[j] = w;
i++;
j--;
}
} while (i <= j);

if (l < j) quickSort(a, l, j);
if (i < r) quickSort(a, i, r);
}

export function main() {
initArr();
quickSort(sortList, 1, SORTELEMENTS);
if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) {
console.log('Validate result error in quicksort');
}
}
3 changes: 3 additions & 0 deletions tests/benchmark/run_benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ let benchmark_options = {
'quicksort': {
wamr_option: [stack_size_option, default_gc_size_option]
},
'quicksort_float': {
wamr_option: [stack_size_option, default_gc_size_option]
},
}

function collect_benchmark_options(options) {
Expand Down

0 comments on commit e130b99

Please sign in to comment.