r/AskComputerScience • • Mar 02 '25
Can algorithmic analysis sometimes be misleading ?
Here are some examples in golang. To be honest these are pretty dumb, but just for argument sake.
Program 1: Initializes an array of length 10, each element set to 1
func main() {
arr := [10]int{}
for i := 0; i < 10; i++ {
arr[i] = 1
}
}
Program 2: Initializes an array of length 1 trillion, each element set to 1
func main() {
arr := [1000000000000]int{}
for i := 0; i < 1000000000000; i++ {
arr[i] = 1
}
}
Aren't both both programs O(1) time and space complexity ? Yet program 2 will use way more RAM than program 1.
To be clear, I'm not arguing complexity analysis is pointless. Just trying to understand analysis from both a theoertical and practical perspective.
0
Upvotes
View all comments
Show parent comment
1
u/knuthf Mar 03 '25
Its weird, they have never considered the compiler, that the code must be generated.
Just initialising huge amounts of data implies that the memory is swapped in and written, swapped out, because none of use have 1TB of RAM.