Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Computer Science Fundamentals for Self-Taught P...
Search
PyCon 2014
April 11, 2014
Programming
4
1.8k
Computer Science Fundamentals for Self-Taught Programmers by Justin Abrahms
PyCon 2014
April 11, 2014
Tweet
Share
More Decks by PyCon 2014
See All by PyCon 2014
Postgres Performance for Humans by Craig Kerstiens
pycon2014
29
3.6k
Technical Onboarding, Training, and Mentoring by Kate Heddleston and Nicole Zuckerman
pycon2014
1
2.3k
"My big gay adventure. Making, releasing and selling an indie game made in python." by Luke Miller
pycon2014
2
1.5k
Farewell and Welcome Home, Python in Two Genders by Naomi_Ceder
pycon2014
1
710
Deliver Your Software in an Envelope by Augie Fackler and Nathaniel Manista
pycon2014
1
520
Hitchhikers Guide to Free and Open Source Participation by Elena Williams
pycon2014
6
1.2k
Localization Revisted (aka. Translations Evolved) by Ruchi Varshney
pycon2014
0
690
Smart Dumpster by Bradley E. Angell
pycon2014
0
490
Software Engineering for Hackers: Bridging the Two Solitudes by Tavish Armstrong
pycon2014
0
710
Other Decks in Programming
See All in Programming
Security_for_introducing_eBPF
kentatada
0
110
CSC305 Lecture 26
javiergs
PRO
0
140
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
280
快速入門可觀測性
blueswen
0
370
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
360
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
3
270
php-conference-japan-2024
tasuku43
0
300
良いユニットテストを書こう
mototakatsu
8
2.4k
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Pragmatic Product Professional
lauravandoore
32
6.3k
A Tale of Four Properties
chriscoyier
157
23k
Optimizing for Happiness
mojombo
376
70k
Into the Great Unknown - MozCon
thekraken
33
1.5k
How GitHub (no longer) Works
holman
311
140k
BBQ
matthewcrist
85
9.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Building Applications with DynamoDB
mza
91
6.1k
Transcript
Justin Abrahms Computer Science for the self taught programmer http://bit.ly/1gfszix
Who am I? • Justin Abrahms • Director of Product
Engineering at Quick Left • Author of Imhotep • @justinabrahms or github://justinabrahms
Overview • How I learn about Big O? • What
is Big O? • How is Big O done? • Resources and learned wisdom
None
A sane data model
Our data model
Our ACTUAL data model
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)
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.
–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.
–Me Big O is how programmers talk about the relation
of how much stuff is being done when comparing two pieces of code.
Google Study List Studied: • Data Structures • Algorithms •
System Design • Java Internals • Concurrency Issues
Google Study List Studied: • Data Structures! • Algorithms! •
System Design • Java Internals • Concurrency Issues These are Big O things
–Wikipedia A data structure is a particular way of storing
and organizing data in a computer so that it can be used efficiently.
–Wikipedia An algorithm is a step-by-step procedure for calculations
O(n)
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
Intentionally left blank for people in the back
def get_from_list(idx, lst): return lst[idx] O(1)
def item_in_list(item, lst): for entry in lst: if entry ==
item: return True return False O(n)
Wait a second…
def item_in_list(item, lst): for entry in lst: if entry ==
item: return True return False O(n) — Broken Down
def item_in_list(item, lst): for entry in lst: O(n) if entry
== item: return True return False O(n) — Broken Down
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
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
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)
Why don’t we say Big O of O(n) * O(1)
+ O(1)?
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)
Example O(n) * O(1) + O(1) Big O: To Plot:
?
Example O(n) * O(1) + O(1) Big O: To Plot:
x * ? O(n) always means x
Example O(n) * O(1) + O(1) Big O: To Plot:
x * 5 + 9 O(1) means pick any constant number
Example To Plot: x * 5 + 9
Example To Plot: x * 5 + 9
Example To Plot: x * 5 + 9
Is it O(1)? To Plot: x * 5 + 9
Is it O(n²)? To Plot: x * 5 + 9
Big O is an approximation of algorithmic complexity
def item_in_list(item, lst): for entry in lst: if entry ==
item: return True return False O(n)
What if the list is empty?
def item_in_list(item, lst): for entry in lst: if entry ==
item: return True return False O(n)
O(log n) The best example of O(log n) is binary
search.
O(log n) 1 2 3 4 5 6 7 8
9 10
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 6?
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 6? Nope.
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 3?
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 3? Nope.
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 4?
O(log n) 1 2 3 4 5 6 7 8
9 10 4 == 4? Yes!
def get_pairs(lst): pair_list = [] for i1 in lst: for
i2 in lst: pair_list.append([i1, i2]) return pair_list ! O(n²)
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²)
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)
O(n²) = O(1) + O(n) * O(n) * O(1) +
O(1)
O(n²) = O(1) + O(n) * O(n) * O(1) +
O(1)
O(n²) = O(1) + O(n) * O(n) * O(1) +
O(1) = O(n) * O(n) * O(1) + O(1) + O(1)
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
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
O(n²) = x² + 29
O(n²) = x² + 29
O(n²) = x² + 29
O(n²) = x² + 29
Gotchas
The Big O of a function might not matter
Theoretical speed is different than practical speed.
This is probably not going to make your app faster.
Resources
Resources http://algorist.com/
Resources https://www.coursera.org/course/algo
Resources https://leanpub.com/computer-science-for-self-taught-programmers/
How do I write my code differently now?
Knowing Big-O doesn’t make you write your code differently.
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
Thanks •
[email protected]
• @justinabrahms • github.com/justinabrahms Credits: ! NYC
slide photo via flickr://Andos_pics