Last active
August 11, 2019 09:42
-
-
Save evan-moon/1d2a10347fffc2363c03d3422f4ad5b9 to your computer and use it in GitHub Desktop.
This file contains 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
let avg = 0; | |
const numbers = []; | |
function average (numbers = []) { | |
const sum = numbers.reduce((prev, current) => prev + current, 0); | |
return sum/numbers.length; | |
} | |
console.time('avg1'); | |
for (let i = 1; i < 100001; i++) { | |
numbers.push(i); | |
avg = average(numbers); | |
} | |
console.timeEnd('avg1'); | |
console.log(`그래서 평균은? -> ${avg}`); | |
let avg2 = 0; | |
function cumulativeAverage (prevAvg, newNumber, listLength) { | |
const oldWeight = (listLength - 1) / listLength; | |
const newWeight = 1 / listLength; | |
return (prevAvg * oldWeight) + (newNumber * newWeight); | |
} | |
console.time('avg2'); | |
for (let i = 1; i < 1000001; i++) { | |
avg2 = cumulativeAverage(avg2, i, i); | |
} | |
console.timeEnd('avg2'); | |
console.log(`그래서 평균은? -> ${avg2}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment