educative.io

Example 1: Measuring Time Complexity, confused about print(sum)

Hi, l have looked carefully on this example. l am confused about the execution cost of print(sum) which is 1. l was thinking that it should be 2 operations.

Hope can see someone tell me right understanding. thx

At the same time, the below are my understandings based on last chapter. If l have anything wrong on understandings, pls correct me!

n = 10 # 1 operation only, variable assignment
sum = 0 #1 operation only, variable assignment
for var in range(n): # consist of no.of vars counts(0,1,…,n-1) which n times totally and
# range(n) executes only once, but cost n
sum += 1 # 3n=size n(assess variable+addition+variable assignment)

print(sum) # ???2 operations, First is variable assess, Second is display value.

l don’t understand that in answer why print(sum) is 1 operation but not 2 operations as same as last question?

Hi yan1 ,

This is Anum from Educative.

print(sum) takes two primitive operations, one for variable access and second for display

We hope Educative has inspired to further your learning.

Best Regards,
Anum | Developer Advocate
educative.io