Advertisement
Palid

Bun wins

Jul 7th, 2022
620
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.56 KB | None
  1. const runThatManyTimes = 100;
  2. const fibSize = 10_000;
  3. const fibRepeats = 1_000;
  4. const executionTimes = new Array(runThatManyTimes);
  5.  
  6. function dev(arr) {
  7.     // Creating the mean with Array.reduce
  8.     let mean =
  9.         arr.reduce((acc, curr) => {
  10.             return acc + curr;
  11.         }, 0) / arr.length;
  12.  
  13.     // Assigning (value - mean) ^ 2 to every array item
  14.     arr = arr.map((k) => {
  15.         return (k - mean) ** 2;
  16.     });
  17.  
  18.     // Calculating the sum of updated array
  19.     let sum = arr.reduce((acc, curr) => acc + curr, 0);
  20.  
  21.     // Calculating the variance
  22.     let variance = sum / arr.length;
  23.  
  24.     // Returning the Standered deviation
  25.     return Math.sqrt(sum / arr.length);
  26. }
  27.  
  28. function runFib(runNumber) {
  29.     const date = Date.now();
  30.  
  31.     for (let a = 0; a < fibRepeats; a++) {
  32.         const fibonacci = new Array(fibSize);
  33.         for (let i = 0; i < fibSize; i++) {
  34.             fibonacci.push(fibonacci[i] + fibonacci[i + 1]);
  35.         }
  36.     }
  37.     const executionTime = Date.now() - date;
  38.     console.log(`Run number: ${runNumber}`);
  39.     executionTimes[runNumber] = executionTime;
  40.     console.log(`Execution time: ${executionTime}ms`);
  41. }
  42.  
  43. for (let runNumber = 0; runNumber < runThatManyTimes; runNumber++) {
  44.     runFib(runNumber);
  45. }
  46.  
  47. const mediumRuntime =
  48.     executionTimes.reduce((acc, v) => acc + v, 0) / executionTimes.length;
  49.  
  50. const standardDeviation = dev(executionTimes);
  51. const min = Math.min(...executionTimes);
  52. const max = Math.max(...executionTimes);
  53.  
  54. console.log(`Mean: ${mediumRuntime}ms`);
  55. console.log(`Standard deviation: ${standardDeviation}ms
  56. `);
  57. console.log(`Max runtime: ${max}ms`);
  58. console.log(`Min runtime: ${min}ms`);
  59.  
Advertisement
RAW Paste Data Copied
Advertisement