Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const runThatManyTimes = 100;
- const fibSize = 10_000;
- const fibRepeats = 1_000;
- const executionTimes = new Array(runThatManyTimes);
- function dev(arr) {
- // Creating the mean with Array.reduce
- let mean =
- arr.reduce((acc, curr) => {
- return acc + curr;
- }, 0) / arr.length;
- // Assigning (value - mean) ^ 2 to every array item
- arr = arr.map((k) => {
- return (k - mean) ** 2;
- });
- // Calculating the sum of updated array
- let sum = arr.reduce((acc, curr) => acc + curr, 0);
- // Calculating the variance
- let variance = sum / arr.length;
- // Returning the Standered deviation
- return Math.sqrt(sum / arr.length);
- }
- function runFib(runNumber) {
- const date = Date.now();
- for (let a = 0; a < fibRepeats; a++) {
- const fibonacci = new Array(fibSize);
- for (let i = 0; i < fibSize; i++) {
- fibonacci.push(fibonacci[i] + fibonacci[i + 1]);
- }
- }
- const executionTime = Date.now() - date;
- console.log(`Run number: ${runNumber}`);
- executionTimes[runNumber] = executionTime;
- console.log(`Execution time: ${executionTime}ms`);
- }
- for (let runNumber = 0; runNumber < runThatManyTimes; runNumber++) {
- runFib(runNumber);
- }
- const mediumRuntime =
- executionTimes.reduce((acc, v) => acc + v, 0) / executionTimes.length;
- const standardDeviation = dev(executionTimes);
- const min = Math.min(...executionTimes);
- const max = Math.max(...executionTimes);
- console.log(`Mean: ${mediumRuntime}ms`);
- console.log(`Standard deviation: ${standardDeviation}ms
- `);
- console.log(`Max runtime: ${max}ms`);
- console.log(`Min runtime: ${min}ms`);
Advertisement
RAW Paste Data
Copied
Advertisement