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

16 comments sorted by

View all comments

Show parent comment

6

u/Han_Sandwich_1907 Mar 02 '25

The point of algorithmic analysis is to look at problems of the form "Do something with <variable size data>," like sort a list with n elements, or color a graph with |V| vertices and |E| edges, or insert an element into a tree with n nodes in k layers. "Initializes an array of length 10, each element set to 1" doesn't really fit into the sort of problems algorithmic analysis deals with.

"Initializes an array (of variable length n), each element set to 1", or "Initializes an array of length 10, each element set to the value k" better fit the idea, where we can see how the algorithm changes as n or k changes.

4

u/[deleted] Mar 02 '25

Do something with <variable size data>

Ah ok, that's what I got wrong.