Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Who am I? • Justin Abrahms • Director of Product Engineering at Quick Left • Author of Imhotep • @justinabrahms or github://justinabrahms

Slide 3

Slide 3 text

Overview • How I learn about Big O? • What is Big O? • How is Big O done? • Resources and learned wisdom

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

A sane data model

Slide 6

Slide 6 text

Our data model

Slide 7

Slide 7 text

Our ACTUAL data model

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

–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.

Slide 11

Slide 11 text

–Me Big O is how programmers talk about the relation of how much stuff is being done when comparing two pieces of code.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Google Study List Studied: • Data Structures! • Algorithms! • System Design • Java Internals • Concurrency Issues These are Big O things

Slide 14

Slide 14 text

–Wikipedia A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Slide 15

Slide 15 text

–Wikipedia An algorithm is a step-by-step procedure for calculations

Slide 16

Slide 16 text

O(n)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Intentionally left blank for people in the back

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

def item_in_list(item, lst): for entry in lst: if entry == item: return True return False O(n)

Slide 21

Slide 21 text

Wait a second…

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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)

Slide 27

Slide 27 text

Why don’t we say Big O of O(n) * O(1) + O(1)?

Slide 28

Slide 28 text

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)

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Example To Plot: x * 5 + 9

Slide 33

Slide 33 text

Example To Plot: x * 5 + 9

Slide 34

Slide 34 text

Example To Plot: x * 5 + 9

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Big O is an approximation of algorithmic complexity

Slide 38

Slide 38 text

def item_in_list(item, lst): for entry in lst: if entry == item: return True return False O(n)

Slide 39

Slide 39 text

What if the list is empty?

Slide 40

Slide 40 text

def item_in_list(item, lst): for entry in lst: if entry == item: return True return False O(n)

Slide 41

Slide 41 text

O(log n) The best example of O(log n) is binary search.

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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²)

Slide 57

Slide 57 text

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)

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

O(n²) = x² + 29

Slide 64

Slide 64 text

O(n²) = x² + 29

Slide 65

Slide 65 text

O(n²) = x² + 29

Slide 66

Slide 66 text

O(n²) = x² + 29

Slide 67

Slide 67 text

Gotchas

Slide 68

Slide 68 text

The Big O of a function might not matter

Slide 69

Slide 69 text

Theoretical speed is different than practical speed.

Slide 70

Slide 70 text

This is probably not going to make your app faster.

Slide 71

Slide 71 text

Resources

Slide 72

Slide 72 text

Resources http://algorist.com/

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

How do I write my code differently now?

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

Thanks • justin@abrah.ms • @justinabrahms • github.com/justinabrahms Credits: ! NYC slide photo via flickr://Andos_pics