use performance.now() instead of Date.now() in benchmark
This commit is contained in:
parent
a839d69c0e
commit
9c79d13b1f
1 changed files with 25 additions and 21 deletions
|
|
@ -5,24 +5,24 @@ function benchmark(N) {
|
|||
// in-place update of native Map: fastest
|
||||
function map() {
|
||||
let t = new Map();
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
t.set(Math.random(), Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durInPlace = (N <= 5000000) ? map() : "(skip)";
|
||||
const durInPlace = (N <= 5000000) ? map() : "";
|
||||
console.log("in-place:", durInPlace, "ms");
|
||||
|
||||
// purely functional red-black tree: slower by constant factor
|
||||
function fun() {
|
||||
let t = createTree();
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
t = t.insert(Math.random(), Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durFunc = fun();
|
||||
|
|
@ -32,27 +32,27 @@ function benchmark(N) {
|
|||
function funNoGC() {
|
||||
const trees = new Array(N);
|
||||
let t = createTree();
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
trees[i] = t; // prevent garbage collection
|
||||
t = t.insert(Math.random(), Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
|
||||
}
|
||||
const durFuncNoGC = (N <= 1000000) ? funNoGC() : "(skip)";
|
||||
const durFuncNoGC = (N <= 1000000) ? funNoGC() : "";
|
||||
console.log("functional red-black (no GC):", durFuncNoGC, "ms");
|
||||
|
||||
// purely functional radix tree
|
||||
function trie() {
|
||||
let t = emptyTrie;
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
// of course we can only insert strings into radix tree:
|
||||
t = insert(t)(String(Math.random()*10))(Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durTrie = trie();
|
||||
|
|
@ -62,52 +62,52 @@ function benchmark(N) {
|
|||
function trieNoGC() {
|
||||
const trees = new Array(N);
|
||||
let t = emptyTrie;
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
trees[i] = t; // prevent garbage collection
|
||||
// of course we can only insert strings into radix tree:
|
||||
t = insert(t)(String(Math.random()*10))(Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durTrieNoGC = (N <= 1000000) ? trieNoGC() : "(skip)";
|
||||
const durTrieNoGC = (N <= 1000000) ? trieNoGC() : "";
|
||||
console.log("functional radix trie (no GC):", durTrieNoGC, "ms");
|
||||
|
||||
// defensive copy of native Map: slowest (won't scale)
|
||||
function copying() {
|
||||
let t = new Map();
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
t = new Map(t);
|
||||
t.set(Math.random(), Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durCopying = (N <= 10000) ? copying() : "(skip)";
|
||||
const durCopying = (N <= 10000) ? copying() : "";
|
||||
console.log("copying:", durCopying, "ms");
|
||||
|
||||
// defensive copy of native Map (no garbage collection): slower than slowest
|
||||
function copyingNoGC() {
|
||||
const maps = new Array(N);
|
||||
let t = new Map();
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
for (let i=0; i<N; ++i) {
|
||||
maps[i] = t; // prevent garbage collection
|
||||
t = new Map(t);
|
||||
t.set(Math.random(), Math.random());
|
||||
}
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
return endTime - startTime;
|
||||
}
|
||||
const durCopyingNoGC = (N <= 10000) ? copyingNoGC() : "(skip)";
|
||||
const durCopyingNoGC = (N <= 10000) ? copyingNoGC() : "";
|
||||
console.log("copying (no GC):", durCopyingNoGC, "ms");
|
||||
|
||||
return [N, durInPlace, durFunc, durFuncNoGC, durTrie, durTrieNoGC, durCopying, durCopyingNoGC];
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
const startTime = performance.now();
|
||||
|
||||
const results = [];
|
||||
const datapoints = [100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000];
|
||||
|
|
@ -118,11 +118,15 @@ for (let i=0; i<datapoints.length; i++) {
|
|||
results.push(benchmark(N));
|
||||
}
|
||||
|
||||
const endTime = Date.now();
|
||||
const endTime = performance.now();
|
||||
|
||||
console.log("Benchmark took", endTime-startTime, "ms");
|
||||
|
||||
console.log();
|
||||
console.log("N;in-place;pure-redblack;pure-redblack-NoGC;pure-radix;pure-radix-NoGC;copying;copying-NoGC");
|
||||
for (const [N, ...rest] of results) {
|
||||
console.log(`${N};${rest.join(';')}`);
|
||||
}
|
||||
console.log();
|
||||
console.log("now go and paste the above CSV in this spreadsheet:");
|
||||
console.log(" https://docs.google.com/spreadsheets/d/1WAl5yCZ0UA0XCBn4xphz4foynQpXih4e59kvkbUANRA/edit?usp=sharing");
|
||||
Loading…
Add table
Add a link
Reference in a new issue