Time Complexity VS Space Complexity

What is the difference between Time Complexity and Space Complexity?

Razni Hashim
3 min readAug 4, 2023

When we talk about how fast a computer program runs and how much memory it needs, we use two important measures:

1. Time complexity

2. Space complexity.

Time complexity tells us how the program’s speed changes with the size of the problem it’s solving, while space complexity tells us how much memory it uses.

Time Complexity

Imagine you have a program that solves a problem, like finding the highest number in a list of numbers.

The time complexity tells you how the time it takes for the program to finish increases when you give it a bigger list.

Let’s say you have a list of 10 numbers, and it takes 1 second for the program to find the highest number.

Now, if you give the program a list of 100 numbers, the time it takes might increase to 10 seconds.

If you give it a list of 1000 numbers, it could take 100 seconds. The time complexity helps us understand how this time grows as the list size increases.

Different types of time complexities are like different ways the program’s speed changes with the problem size:

  1. Constant Time: The program always takes the same time, no matter how big the list is. For example, it might always take 1 second, even if you have 10 or 1000 numbers.
  2. Logarithmic Time: As the list gets bigger, the time it takes increases, but not too much.
  3. Linear Time: The time it takes grows linearly with the list size. If you have 10 times more numbers, it takes about 10 times more time.
  4. Quadratic Time: The time grows much faster with the list size. If you have 10 times more numbers, it takes about 100 times more time.
  5. Exponential Time: The time doubles with each addition to the list. This means even a small increase in the list size can make the program incredibly slow.

Space Complexity

When the program runs, it needs to store data in the computer’s memory. The space complexity tells us how much memory the program needs as the problem size increases.

Using the previous example of finding the highest number in a list, space complexity would be how much extra memory the program needs to store the list.

  1. Constant Space: The program uses the same amount of memory, no matter how big the list is. It doesn’t need any extra memory for larger lists.
  2. Logarithmic Space: The memory usage grows slowly with the list size, similar to logarithmic time complexity.
  3. Linear Space: The memory usage increases in a straight line with the list size. For larger lists, it needs more memory, but it’s still manageable.
  4. Quadratic Space: The memory usage grows much faster with the list size. For bigger lists, it needs a lot more memory.
  5. Exponential Space: The memory needed doubles with each addition to the list. This can become a significant problem when the list is even slightly larger.

Time complexity measures how the program’s speed changes as the problem size increases, and space complexity measures how much extra memory the program needs as the problem size grows. Hope you gained some knowledge on the topic.

--

--