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
Look ma' I know my algorithms!
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Lucia Escanellas
October 24, 2014
Programming
490
7
Share
Look ma' I know my algorithms!
RubyConf Argentina 2014
Lucia Escanellas
October 24, 2014
Other Decks in Programming
See All in Programming
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
1.1k
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
320
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
140
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
2.6k
tRPCの概要と少しだけパフォーマンス
misoton665
2
270
ハーネスエンジニアリングとは?
kinopeee
13
6.8k
空間オーディオの活用
objectiveaudio
0
140
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
170
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
770
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
3
750
Making the RBS Parser Faster
soutaro
0
670
Are We Really Coding 10× Faster with AI?
kohzas
0
130
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.8k
ラッコキーワード サービス紹介資料
rakko
1
3.2M
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
910
Faster Mobile Websites
deanohume
310
31k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
Color Theory Basics | Prateek | Gurzu
gurzu
0
310
Code Review Best Practice
trishagee
74
20k
Thoughts on Productivity
jonyablonski
76
5.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
180
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
160
Raft: Consensus for Rubyists
vanstee
141
7.4k
Transcript
Look ma’, I know my algorithms!
Lucia Escanellas raviolicode
Attributions https://flic.kr/p/6DDvQP https://flic.kr/p/qv5Zp https://flic.kr/p/6SaZsP https://flic.kr/p/edauSN https://flic.kr/p/4uNfK8 https://flic.kr/p/o9ggdk https://flic.kr/p/6kfuHz https://flic.kr/p/5kBtbS
Speed Speed
Zen Elegance Elegance
Toolbox
Theory Theory
This example Not so common
FROM >30HS TO 18 S
WHY USE ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY
WHY USE ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY
WHY USING ORDERS? ALGORITHMS ARE POWERFUL AVOID TRAPS IN RUBY
Let’s have a look at the PROBLEM
Ordered array How many pairs (a,b) where a ≠ b
-100 <= a + b <= 100
Array: [-100, 1, 100]
Array: [-100, 1, 100] (-100, 1), (-100, 100), (1, 100)
Array: [-100, 1, 100] (-100, 1), (-100, 100), (1, 100)
-100 + 1 = 99 YES
Array: [-100, 1, 100] (-100, 1), (-100, 100), (1, 100)
-100 + 100 = 0 YES
Array: [-100, 1, 100] (-100, 1), (-100, 100), (1, 100)
1 + 100 = 101 NO
Array: [-100, 1, 100] (-100, 1), (-100, 100), (1, 100)
Result: 2
First solution Combinations of 2 elements Filter by: -100 <=
a + b <= 100
def count! combinations = @numbers.combination(2).to_a! ! combinations! .map{ |a,b| a
+ b }! .select do |sum|! sum.abs <= THRESHOLD! end.size! end
10K takes 10s BUT 100M takes 30hs
Time to buy a NEW LAPTOP!
Big O notation How WELL an algorithm SCALES as the
DATA involved INCREASES
Calc Array size (length=N) Count elements one by one: O(N)
Calc Array size (length=N) Count elements one by one: O(N)
Length stored in variable: O(1)
Graphical Math Properties Order Mathematical Properties
Remember: f < g => O(f + g) = O(g)
O(K . f) = O(f) O(1) < O(ln N) < O(N) < O(N2) < O(eN)
Ex: Binary Search Find 7 in [1, 2, 3, 4,
5, 6, 7, 8] 1. element in the middle is 5 2. 5 == 7 ? NO 3. 5 < 7 ? YES => Find 7 in [6, 7, 8] Step 1
! Find 7 in [0, 1, 2, 3, 4, 5,
6, 7, 8] 1. element in the middle is 7 2. 7 == 7 ? YES! FOUND IT!! Step 2
Ex: Binary Search Worst case: ceil ( Log2 N )
23 = 8 ONLY 3 steps
Typical examples Access to a Hash O(1) Binary search O(log
N) Sequential search O(N) Traverse a matrix NxN O(N2)
DON’T JUST BELIEVE ME fooplot.com
BUT raviolicode, I’m getting BORED
I WANT CONCURRENCY I WANT CONCURRENCY
wait… was it Concurrency? or Parallelism?
None
None
None
None
None
None
GIL+CPU-bound NO I/O OPERATIONS concurrency = OVERHEAD
NOT what I was expecting
Parallelism... Parallelism
None
What do we REALLY get? O(N2 / cores) = O(N
2 ) jRubyGo Scala
NO Spoilers O(N2) O(N.log(N)) O(N)
THINKING algorithms is as IMPORTANT as ANY OTHER technique
BYE.
Wait! It's still slow. Wait! It’s still SLOW
Given [1,2,3,4,5] Take 1, then print [5,4,3,2] Take 2, then
print [5,4,3] and so on…
What’s the ORDER of this code? @nums.each_with_index do |a,i|! !
puts @nums.slice(i+1,N).reverse! .inspect! end
What’s the ORDER of this code? @nums.each_with_index do |a,i|! !
puts @nums.slice(i+1,N).reverse! .inspect! end Looks like O(N)
What’s the ORDER of this code? @nums.each_with_index do |a,i|! !
puts @nums.slice(i+1,N).reverse! .inspect! end Behaves like O(N2)
Let’s Look at the DOCS Ruby-Doc.org ! #reverse
O(N) hidden! O(N)!
What’s the ORDER of this code? @nums.each_with_index do |a,i|! !
puts @nums.slice(i+1,N).reverse! .inspect! end O(N2)!
Leaky abstractions LEAKY ABSTRACTIONS
All Non-trivial abstractions are LEAKY to some degree
ABSTRACTIONS DO NOT really SIMPLIFY as they were meant to
Knowing THE ALGORITHMS Behind everyday methods PAYS OFF
Thanks :) Thanks :)