![Page 1: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/1.jpg)
Why Python, Ruby, and Javascript are slow
Alex GaynorWaza 2013
![Page 2: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/2.jpg)
You may know me from...
![Page 3: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/3.jpg)
rdio.com
![Page 4: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/4.jpg)
• CPython
• Django
• PyPy
![Page 5: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/5.jpg)
Twitter rants about how computers are bad
![Page 6: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/6.jpg)
Topaztopazruby.com
![Page 7: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/7.jpg)
There is no benchmark but your benchmark
![Page 8: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/8.jpg)
Lame Excuses about why they’re Slow
![Page 9: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/9.jpg)
Dynamic Typing
![Page 10: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/10.jpg)
“You can monkey patch anything”
![Page 11: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/11.jpg)
Harder to Optimizevs.
Slow
![Page 12: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/12.jpg)
The Truth
![Page 13: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/13.jpg)
Let’s talk about C
![Page 14: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/14.jpg)
struct Point { double x; double y; double z;};
![Page 15: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/15.jpg)
class Point(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z
![Page 16: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/16.jpg)
data = { "x" x, "y": y, "z": z,}
![Page 17: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/17.jpg)
Dictionaryvs.
Object
![Page 18: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/18.jpg)
std::hash_set<std::string, double> point;point["x"] = x;point["y"] = y;point["z"] = z;
![Page 19: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/19.jpg)
And it would be slow
![Page 20: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/20.jpg)
Why don’t people care?
![Page 21: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/21.jpg)
Let’s talk about strings
![Page 22: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/22.jpg)
Given a string matching: “\w+-\d+”
return the integral part of the value
![Page 23: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/23.jpg)
int(s.split("-", 1)[1])
![Page 24: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/24.jpg)
atoi(strchr(s, '-') + 1)
![Page 25: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/25.jpg)
Things that take time
• Hash table lookups
• Allocations
• Copying
![Page 26: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/26.jpg)
The C way:
BYOB
![Page 27: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/27.jpg)
char *data = malloc(1024);while (true) { read(fd, data, 1024); char *start = data; while (start < data + 1024) { if (isspace(*start)) { break; } start++; } printf("%s\n", start);}
![Page 28: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/28.jpg)
while True: data = os.read(fd, 1024) print data.lstrip()
![Page 29: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/29.jpg)
long *squares(long n) { long *sq = malloc(sizeof(long) * n); for (long i = 0; i < n; i++) { sq[i] = i * i; } return sq;}
![Page 30: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/30.jpg)
def squares(n): sq = [] for i in xrange(n): sq.append(i * i) return sq
![Page 31: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/31.jpg)
Missing APIs
![Page 32: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/32.jpg)
from __pypy__ import newlist_hintdef squares(n): sq = newlist_hint(n) for i in xrange(n): sq.append(i * i) return sq
![Page 33: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/33.jpg)
Don’t make us add heuristics
![Page 34: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/34.jpg)
Heuristics = WAG
![Page 35: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/35.jpg)
Growing divide between optimizing and
not
![Page 36: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/36.jpg)
Recap
• Line for line these languages are fast!
• Take care in data structures (data structure heuristics are the WORST)
• We need better no-copy/preallocate APIs
![Page 37: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/37.jpg)
Don’t abandon beauty, simplicity, our values for
performance
Make performance beautiful.
![Page 38: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/38.jpg)
Thank you!https://speakerdeck.com/alex
@alex_gaynor
![Page 39: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow](https://reader034.vdocuments.mx/reader034/viewer/2022051610/5485909fb47959ce0c8b4e72/html5/thumbnails/39.jpg)
If there’s time
• Java collections vs. Array and Hash. Need more choices.
• Stop writing C extensions, use something like cffi
• Teach good benchmarking practices