Advertisement
Palid

Node wins

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