Created
October 8, 2022 06:25
-
-
Save nexpr/d47b98488bf3af5edb7f694c07739ef8 to your computer and use it in GitHub Desktop.
複数の関数の実行時間計測
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (fns, measure_count, effective_rate) => { | |
const data = [] | |
for (let i = 0; i < measure_count; i++) { | |
for (const [fn_index, fn] of fns.entries()) { | |
if (!data[fn_index]) { | |
data[fn_index] = [] | |
} | |
const start = performance.now() | |
fn() | |
const end = performance.now() | |
data[fn_index].push(end - start) | |
} | |
} | |
return data.map((fn_data, fn_index) => { | |
return { | |
name: fns[fn_index].name, | |
...agg(fn_data, effective_rate), | |
} | |
}) | |
} | |
const agg = (data, effective_rate) => { | |
const sorted = data.sort((a, b) => a - b) | |
const max = sorted.at(-1) | |
const times = getEffective(sorted, effective_rate) | |
const num = times.length | |
const range = [times[0], times.at(-1)] | |
const avg = times.reduce((a, b) => a + b, 0) / num | |
const med = num % 2 === 0 ? ((times[num / 2] + times[num / 2 - 1]) / 2) : times[~~(num / 2)] | |
return { range, max, avg, med, num } | |
} | |
const getEffective = (data, rate) => { | |
const fixed_rate = Math.max(0, Math.min(100, rate)) | |
const effective_num = ~~(data.length * fixed_rate / 100) | |
const remove_num = data.length - effective_num | |
const edge = ~~(remove_num / 2) | |
return data.slice(edge, edge + effective_num) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment