Save 37% off PRO during our Black Friday Sale! »

Computer Science Fundamentals for Self-Taught Programmers by Justin Abrahms

Computer Science Fundamentals for Self-Taught Programmers by Justin Abrahms

D21717ea76044d31115c573d368e6ff4?s=128

PyCon 2014

April 11, 2014
Tweet

Transcript

  1. Justin Abrahms Computer Science for the self taught programmer http://bit.ly/1gfszix

  2. Who am I? • Justin Abrahms • Director of Product

    Engineering at Quick Left • Author of Imhotep • @justinabrahms or github://justinabrahms
  3. Overview • How I learn about Big O? • What

    is Big O? • How is Big O done? • Resources and learned wisdom
  4. None
  5. A sane data model

  6. Our data model

  7. Our ACTUAL data model

  8. What does N+1 Selects look like? entries = get_post_ids() final_list

    = [] for entry_id in entries: entry = get_entry(entry_id) final_list.append(entry)
  9. Questions • How did I miss this? • How did

    this guy know about it and I didn’t? • How can I make sure this never happens again.
  10. –Wikipedia (aka Inducer of Impostor Syndrome) In mathematics, big O

    notation describes the limiting behavior of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions.
  11. –Me Big O is how programmers talk about the relation

    of how much stuff is being done when comparing two pieces of code.
  12. Google Study List Studied: • Data Structures • Algorithms •

    System Design • Java Internals • Concurrency Issues
  13. Google Study List Studied: • Data Structures! • Algorithms! •

    System Design • Java Internals • Concurrency Issues These are Big O things
  14. –Wikipedia A data structure is a particular way of storing

    and organizing data in a computer so that it can be used efficiently.
  15. –Wikipedia An algorithm is a step-by-step procedure for calculations

  16. O(n)

  17. What sorts of Big O are there? • O(1) —

    Constant Time • O(log n) — Logarithmic Time • O(n) — Linear Time • O(n²) — Quadratic Time • O(n!) — Factorial Time
  18. Intentionally left blank for people in the back

  19. def get_from_list(idx, lst): return lst[idx] O(1)

  20. def item_in_list(item, lst): for entry in lst: if entry ==

    item: return True return False O(n)
  21. Wait a second…

  22. def item_in_list(item, lst): for entry in lst: if entry ==

    item: return True return False O(n) — Broken Down
  23. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: return True return False O(n) — Broken Down
  24. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True return False O(n) — Broken Down
  25. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True O(1) return False O(1) O(n) — Broken Down
  26. def item_in_list(item, lst): for entry in lst: O(n) if entry

    == item: O(1) return True O(1) return False O(1) O(n) — Broken Down =O(n) * O(1) + O(1)
  27. Why don’t we say Big O of O(n) * O(1)

    + O(1)?
  28. In non-“math-y” terms • If we plot our function, we

    can also plot M * the big O and end up with a line that our function never crosses (for certain values of X)
  29. Example O(n) * O(1) + O(1) Big O: To Plot:

    ?
  30. Example O(n) * O(1) + O(1) Big O: To Plot:

    x * ? O(n) always means x
  31. Example O(n) * O(1) + O(1) Big O: To Plot:

    x * 5 + 9 O(1) means pick any constant number
  32. Example To Plot: x * 5 + 9

  33. Example To Plot: x * 5 + 9

  34. Example To Plot: x * 5 + 9

  35. Is it O(1)? To Plot: x * 5 + 9

  36. Is it O(n²)? To Plot: x * 5 + 9

  37. Big O is an approximation of algorithmic complexity

  38. def item_in_list(item, lst): for entry in lst: if entry ==

    item: return True return False O(n)
  39. What if the list is empty?

  40. def item_in_list(item, lst): for entry in lst: if entry ==

    item: return True return False O(n)
  41. O(log n) The best example of O(log n) is binary

    search.
  42. O(log n) 1 2 3 4 5 6 7 8

    9 10
  43. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  44. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  45. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 6?
  46. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 6? Nope.
  47. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  48. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  49. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 3?
  50. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 3? Nope.
  51. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  52. O(log n) 1 2 3 4 5 6 7 8

    9 10 4
  53. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 4?
  54. O(log n) 1 2 3 4 5 6 7 8

    9 10 4 == 4? Yes!
  55. def get_pairs(lst): pair_list = [] for i1 in lst: for

    i2 in lst: pair_list.append([i1, i2]) return pair_list ! O(n²)
  56. def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

    O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²)
  57. def get_pairs(lst): pair_list = [] O(1) for i1 in lst:

    O(N) for i2 in lst: O(N) pair_list.append([i1, i2]) O(1) return pair_list O(1) ! O(n²) = O(1) + O(n) * O(n) * O(1) + O(1)
  58. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1)
  59. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1)
  60. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1)
  61. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13
  62. O(n²) = O(1) + O(n) * O(n) * O(1) +

    O(1) = O(n) * O(n) * O(1) + O(1) + O(1) = x * x + 7 + 9 + 13 = x² + 29
  63. O(n²) = x² + 29

  64. O(n²) = x² + 29

  65. O(n²) = x² + 29

  66. O(n²) = x² + 29

  67. Gotchas

  68. The Big O of a function might not matter

  69. Theoretical speed is different than practical speed.

  70. This is probably not going to make your app faster.

  71. Resources

  72. Resources http://algorist.com/

  73. Resources https://www.coursera.org/course/algo

  74. Resources https://leanpub.com/computer-science-for-self-taught-programmers/

  75. How do I write my code differently now?

  76. Knowing Big-O doesn’t make you write your code differently.

  77. Big O is… • useful in communicating about complexity of

    code • basic arithmetic and algebra • used in talking about algorithms and data structures • not as hard as it originally sounds
  78. Thanks • justin@abrah.ms • @justinabrahms • github.com/justinabrahms Credits: ! NYC

    slide photo via flickr://Andos_pics