Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Make your Big O smaller, complexity explained

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
Tweet

More Decks by Meili Triantafyllidi

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. Time complexity

    View full-size slide

  6. Time complexity
    problem size
    ? time

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

  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)

    View full-size slide

  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 !!!!!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  39. Thank you
    @meilitr

    View full-size slide