Time Complexity and Space Complexity
Analyzing the efficiency of an algorithm in terms of how much time and space (memory) they require as the input size grows.
Time complexity refers to the amount of time an algorithm takes to complete as a function of the size of the input data. It's typically expressed using Big O notation, which describes the upper bound of the time required in the worst-case scenario.
2. Space Complexity
Space complexity refers to the amount of memory an algorithm uses as a function of the input size. It also uses Big O notation to describe the upper bound of the memory required.
领英推荐
Example
Consider a simple loop that sums up an array of numbers:
def sum_array(arr):
total = 0 # O(1) space
for num in arr: # O(n) time
total += num # O(1) time
return total