4 ms·
OK I had to write things down to try and make sense of this. If anyone is like me, consider this loop that loops 5 times... maybe it will help? defines = 0
by nickloewen 9y ago
OK I had to write things down to try and make sense of this. If anyone is like me, consider this loop that loops 5 times... maybe it will help?
defines = 0
tests = 0
increments = 0
defines++
for (let i = 0; i < 5; i++) {
tests++
increments++
}
console.log(`defines: ${defines}, tests: ${tests}, increments: ${increments}`)
For the sake of space I'm not going too copy nested versions of that, but imagine nesting it two and then three levels deep.
1 level will increment 5 (5^1) times
2 levels will increment 25 (5^2) times
3 levels will increment 125 (5^3) times
More interesting are the number of definitions and tests:
levels tests defines
1 15 1
2 30 6
3 155 31
But I don't know is how the interpreter works and whether it has tricks to shrink those numbers; those just reflect my naive mental model of how things work.
- whatyoucantsay 9y agoThe confusion here is about math, not syntax. N^2 is polynomial. 2^N is exponential. https://stackoverflow.com/questions/4317414/polynomial-time-and-exponential-time https://stackoverflow.com/questions/4317414/polynomial-time-...