Skip to content
BENIBIX Learn

Time vs Space Complexity

Time complexity counts steps. Space complexity counts memory. Both say how the cost grows as the input grows, and a faster way often needs more memory.

Development🧮 Algorithms & Data Structures

Time Complexity

How much more work as the input grows. It is written in Big O.

Time complexity is how the number of steps an algorithm takes grows with the size of its input. It is written in Big O, like O(n) or O(n log n). It counts steps rather than seconds, so it does not depend on how fast the computer is. Looking up a key in a hash table is O(1) on average. Sorting with merge sort is O(n log n).

Example
Emma's loop inside a loop checks every pair of 1,000 users. That is O(n²), about 1,000,000 checks.
Why it matters
It tells you whether code that runs fine on 100 rows will still run on 10 million.
Full entry
Development🧮 Algorithms & Data Structures

Space Complexity

How much extra memory a task needs. It grows with the input.

Space complexity is how much extra memory an algorithm needs as its input grows. Like time complexity, it is written in Big O. Sorting a list in place may need O(1) extra memory. Making a full copy of the list needs O(n). Often you can trade one for the other: use more memory to save time, or the reverse.

Example
Jack's first version copied a 2 GB file into memory to count its lines. Reading it one line at a time cut the memory to a few kilobytes.
Why it matters
Memory runs out before time does on phones, small servers and huge data sets.
Full entry

In one line

Time Complexity
How much more work as the input grows. It is written in Big O.
Space Complexity
How much extra memory a task needs. It grows with the input.