Calculating time complexity in Python is very easy by comparing the time it takes to run an algorithm vs the size of the input. We can do something like:
import time
start = time.time()
<Run the algorithm on input_n (input of size n)>
end = time.time()
time_n = end - start
By graphing time_n
vs input_n
, we can observe whether the time complexity is constant, linear, exponential, etc.
Is there a similarly empirical, programmatic way of calculating the space complexity of an algorithm in Python, where we can measure the amount of space used as the input size grows?