Meili Triantafyllidi
February 21, 2019
22

Make your Big O smaller, complexity explained

Ever heard of big O and it sounded chinese/greek? Programmers that do not come from computer science do not know what this is. In this talk big O complexity is explained and displayed with examples in Python language how it can affect code execution. I give tips of how to make python code have better performance and why they they work.

Meili Triantafyllidi

February 21, 2019

Transcript

1. Make your Big O smaller
complexity explained
Meili Triantafyllidi - @meilitr

2. set or dict or
list or
tuple...?

3. set or dict or
list or
tuple...?
How it will
perform with
more data?

4. Notation
O(n), O(n2), O(nlogn), O(1) ..

5. Time complexity

6. Time complexity
problem size
? time

7. O(n) - Linear complexity
● 1 elements X time
● 2 elements 2X time
● 10 elements 10X time
● 100 elements 100X time

8. O(n) - Linear complexity
N
Time
N
Time
N
Time
Time = 3N Time = 0.5N
Time = N

9. O(n) - Linear complexity
for i in range(n):
print(i)

10. O(n) - Linear complexity
sum(lst)

11. O(n2) - Quadratic complexity
● 1 elements X time
● 2 elements 4X time
● 10 elements 100X time
● 100 elements 10000X time

12. O(n2) - Quadratic complexity
● 1 elements X time
● 2 elements 4X time
● 10 elements 100X time
● 100 elements 10000X time ←100 slower than O(n)

13. O(n2) - Quadratic complexity
# Sequences of 2
for i in range(n):
for k in range(n):
print(i, k)

14. O(n2) - Quadratic complexity
# Sequences of 2
for i in range(n):
for k in range(n):
print(i, k)
O(n)

15. O(n2) - Quadratic complexity
# Sequences of 2
for i in range(n):
for k in range(n):
print(i, k)
O(n2)

16. O(1) - constant complexity
● 1 elements X time
● 2 elements X time
● 10 elements X time
● 100 elements X time

17. O(1) - constant complexity
● 1 elements X time
● 2 elements X time
● 10 elements X time
● 100 elements X time
O(1)

18. O(1) - constant complexity
# d: dict or set
key in d

19. O(1) - constant complexity
# d: dict or set
key in d
dict

20. Common pitfall
key in d
# O(1)

21. Common pitfall
key in lst
# O(n)
key in d
# O(1)

22. Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)

23. Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)
O(n)

24. O(n2)
Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)

25. O(n2)
Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)
O(1)

26. O(n)
O(n2)
Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)

27. O(n)
O(n2)
Context matters
for i in range(n):
if i in lst:
print(i)
vs
for i in range(n):
if i in d:
print(i)
n=100 -> 100 slower !!!!!

28. Context matters
s = set(lst) # O(n)
key in s # O(1)

29. Context matters
s = set(lst) # O(n)
key in s # O(1)
O(n)

30. do_a() # O(n)
do_b() # O(n)
do_c() # O(n2)
Context matters

31. O(n2)
Context matters
do_a() # O(n) or O(1)
do_b() # O(n) or O(1)
do_c() # O(n2)
No difference

32. Common pitfall
a += "text" # O(len(a))

33. Common pitfall
for name in lst:
a += name
''.join(lst)

34. Common pitfall
# list add/remove intermediate
lst.pop(1) # O(n)
lst.insert(10) # O(n)

35. Common pitfall
# list add/remove intermediate
lst.pop(1) # O(n)
lst.insert(10) # O(n)
dict, set, frozenset, collections.deque,
priority queue (heapq)

36. https://wiki.python.org/moin/TimeComplexity

37. https://wiki.python.org/moin/TimeComplexity

38. Summary
● Know your data structures
○ set, dictionaries are our friends for lookups
○ Lists are good for appends
○ Search python complexity
● Look for O(n) → O(1) where it reduces total complexity
● Beware of hidden complexity in assignments

39. Thank you
@meilitr