App-Cheats
view release on metacpan or search on metacpan
#############################################################
# Time a function in javascript (benchmark)
# Not really accurate
const startTime = new Date().getTime();
const endTime = new Date().getTime();
console.log("sort function took " + (endTime-startTime) + "(ms)")
# Function for benchmarking different code snippets (timing,testing,profiling)
# Simple.
function benchmark(functions,iterations=1) {
for(const code of functions){ // foreach loop in javascript
const name = code.name; // Code refence to name
console.time(name); // Timing/benchmarking function
for(let i = 1; i <= iterations; i++ )
code();
console.timeEnd(name);
}
}
# Usage
benchmark([get_head_checkbox, get_head_checkbox2]);
benchmark([get_head_checkbox, get_head_checkbox2],10000);
# Function for benchmarking different code snippets (timing,testing,profiling)
# Includes percentages and sorted.
// function benchmark() {
// // Get inputs
// let code_refs = [...arguments];
// let iterations = 1;
// const last_index = code_refs.length - 1;
//
// // Check last input
// if(typeof(code_refs[last_index]) == "number")
// iterations = code_refs.pop();
//
// // Check for tests
// if(!code_refs.length)
// return;
//
// const stats = [];
//
// // Get statistics
// for(const code of code_refs){ // foreach loop in javascript
// const t0 = performance.now(); // High precision time
// for(let i = 1; i <= iterations; i++ ) // Run a specified amount of times
// code();
// const time = (performance.now()-t0).toFixed(2);
// stats.push({name: _get_code_name(code), time: time});
// }
//
// // Sort statistics: fastest (lowest time) first
// stats.sort((a,b) => (Number(a.time) >= Number(b.time)) ? 1 : -1);
//
// // Baseline to compare with
// const longest_time = stats[stats.length-1]["time"];
s/(.)\g1+/$1/;
$_;
},
tr => sub{
local $_ = $copy;
tr///cs;
$_;
}
}, 1000000
'
(warning: too few iterations for a reliable count)
Rate s tr
s 1408451/s -- -70%
tr 4761905/s 238% --
# Comparing different ways in perl to combine hashes.
#
# Each: 2.6s
while ( my ($key,$val) = each %users_one ) {
$users{$key} = $val;
}
( run in 0.645 second using v1.01-cache-2.11-cpan-96521ef73a4 )