Created
February 20, 2022 04:51
-
-
Save Ruffo324/dc1905a28a6290a4053fcb35d16f6e54 to your computer and use it in GitHub Desktop.
@SlothOath - avereage temperature amplitude - possible solutions with benchmarks
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
const temperatures = [3, -2, -6, -1, 'error', 9, 13, 17, 15, 14, 9, 5, "", undefined, null, NaN], | |
benchmarkRunFuncXTimes = 1000, | |
toBenchmark = {}; | |
// =========================================================================================== | |
// Mögliche Lösungen: | |
// =========================================================================================== | |
toBenchmark.filterSortLastAndFirst = function (range) { | |
let minAndMax = range.filter(t => !isNaN(t)).sort((a, b) => a - b); | |
return minAndMax.at(-1) - minAndMax.at(0); | |
} | |
toBenchmark.onlySort = function (range) { | |
let minAndMax = range.sort((a, b) => a - b); | |
return minAndMax.at(-1) - minAndMax.at(0); | |
} | |
toBenchmark.calcAmplitudeVonSlothOath = function (temperatures) { | |
let lowestTemp = 1000; | |
let highestTemp = -1000; | |
for (let i = 0; i < temperatures.length; i++) { | |
if (temperatures[i] === 'error') continue; | |
if (temperatures[i] < lowestTemp) lowestTemp = temperatures[i]; | |
if (temperatures[i] > highestTemp) highestTemp = temperatures[i]; | |
} | |
return highestTemp - lowestTemp; | |
} | |
// =========================================================================================== | |
// Benchmark stuff..: | |
// =========================================================================================== | |
/** 🧐 Funktion wird nur zum Vergleichen der Performance von anderen Funktionen benötigt. */ | |
const longestFunctionNameLength = Object.getOwnPropertyNames(toBenchmark) | |
.sort((a, b) => a.length - b.length).at(-1).length; | |
const longestNumberLength = `${Number.MAX_VALUE}`.length - 1; | |
function benchmarkFunction(func, funcName) { | |
let result = func(temperatures); | |
let runtimes = [...Array(benchmarkRunFuncXTimes).keys()].map(_ => { | |
let began = performance.now(); | |
func(temperatures) | |
let done = performance.now(); | |
return (done - began); | |
}); | |
let averageRuntime = runtimes.reduce((a, b) => a + b) / runtimes.length; | |
console.log(`| ${funcName.padEnd(longestFunctionNameLength)} | ${result.toString().padEnd(longestNumberLength)} | ${averageRuntime.toString().padEnd(longestNumberLength)} |`); | |
} | |
console.log(`\nDurchschnittliche Laufzeiten bei jeweils ${benchmarkRunFuncXTimes} durchläufen:\n`) | |
let tableHeader = `| ${"Function Name".padEnd(longestFunctionNameLength)} | ${"Ergebnis ".padEnd(longestNumberLength)} | ${"Laufzeit in ms ".padEnd(longestNumberLength)} |`; | |
console.log("".padEnd(tableHeader.length, '-')) | |
console.log(tableHeader) | |
console.log("".padEnd(tableHeader.length, '-')) | |
for (let i in toBenchmark) | |
benchmarkFunction(toBenchmark[i], i); | |
console.log("".padEnd(tableHeader.length, '-')) |
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
"C:\Program Files\nodejs\node.exe" C:\_Files\0_Programming\1_Playground\nodejs\app.js | |
Durchschnittliche Laufzeiten bei jeweils 1000 durchläufen: | |
------------------------------------------------------------------------------- | |
| Function Name | Ergebnis | Laufzeit in ms | | |
------------------------------------------------------------------------------- | |
| filterSortLastAndFirst | 23 | 0.005349499702453613 | | |
| onlySort | NaN | 0.002023699402809143 | | |
| calcAmplitudeVonSlothOath | 23 | 0.00390200012922287 | | |
------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment