Search⌘ K

Example: Measuring Time Complexity of For Loop

Explore how to measure the time complexity of a simple for loop by breaking down each operation, counting executions, and summing them to determine overall running time. This lesson helps you understand the principles behind evaluating algorithm performance step-by-step.

Lets now calculate the running time complexity of a more complex program. We will split the code into individual operations and then compute how many times each is executed.

Simple For Loop of Size n #

Here is an example of a simple loop of size n:

Python 3.5
n = 10 # just as an example, n can be anything
sum = 0
for var in range(n):
sum += 1
print(sum)
Operation Number of executions
n = 10 1
sum = 0 1
range(n) 11
var=0 1
var=1 1
var=2 1
var=n-1 1
...