| 1 |
SF Python 2015 Lightning Talk |
| 2 |
============================= |
| 3 |
|
| 4 |
* `Accompanying Slides`_ |
| 5 |
|
| 6 |
Good evening! I'm Grant Jenks. Let's talk about a missing battery. |
| 7 |
|
| 8 |
My journey started here. From collections import sorted |
| 9 |
dictionary. Oops. That's an import error. Import sorted list. Import |
| 10 |
error. Import sorted set. Import error. |
| 11 |
|
| 12 |
I don't always import sorted types. But when I do, I expect them in the |
| 13 |
standard lib. |
| 14 |
|
| 15 |
And why would I expect that? I don't know. I guess PHP and Javascript don't |
| 16 |
have 'em either. |
| 17 |
|
| 18 |
But the chorus and the refrain from core developers is: "Look to the PyPI." Ok, |
| 19 |
good advice. |
| 20 |
|
| 21 |
Here's what that's like. It's like walking into my daughter's room after she's |
| 22 |
dumped all her toys on the floor. Seriously. PyPI has got to work better than |
| 23 |
using Google with the site operator. |
| 24 |
|
| 25 |
I love Python because there's one right way to do things. Just give me sorted |
| 26 |
types. |
| 27 |
|
| 28 |
I'm not the only one asking. Neil asked this in 2011. And Nick answered. "If a |
| 29 |
user is sophisticated enough to realise that the builtin types aren't the right |
| 30 |
solution for their problem, then they're also up to the task of finding an |
| 31 |
appropriate third party library." |
| 32 |
|
| 33 |
Okay. Fine. But just tell me the right answer. Give me a standard-lib-plus-plus |
| 34 |
or something. |
| 35 |
|
| 36 |
I couldn't find the right answer so I built it. Here it is. Sorted |
| 37 |
Containers. The right answer is pure-Python. It's Python 2 and Python 3 |
| 38 |
compatible. It's fast. It's fully-featured. And it's extensively tested with |
| 39 |
100% coverage and hours of stress. |
| 40 |
|
| 41 |
Performance is a feature. That means graphs. Lots of them. Here's the |
| 42 |
performance of creating a sorted list from random numbers. I benchmark Sorted |
| 43 |
Containers against every worthy competitor I can find. |
| 44 |
|
| 45 |
Here we see it's five to ten times faster than sortedcollection which is in |
| 46 |
fact a little recipe Raymond wrote and linked from the Python docs. |
| 47 |
|
| 48 |
Sorted dictionaries have the most competition. And the performance of Sorted |
| 49 |
Containers is great. And it's not just initializing that's fast. Every common |
| 50 |
method is benchmarked for you. |
| 51 |
|
| 52 |
Here's a quick look at sorted sets and your options. The blist module is kind |
| 53 |
of the incumbent. It works great as a specialized list. But please stop using |
| 54 |
it for sorted types. |
| 55 |
|
| 56 |
There are also benchmarks across runtimes. When you write pure-Python code, it |
| 57 |
really screams on PyPy. Notice where the JIT kicks in around a hundred thousand |
| 58 |
list items. The speedup is greater than 10x. |
| 59 |
|
| 60 |
In addition to the typical sequence, mapping, and set apis, there's a number of |
| 61 |
bonus methods which are possible because of the sort order constraint. |
| 62 |
|
| 63 |
For example, sorted lists include bisect methods and there's an islice method |
| 64 |
that implements slice indexing but returns an iterator. |
| 65 |
|
| 66 |
Sorted dictionaries also support integer indexing. You can lookup the index of |
| 67 |
a key or bisect keys in a mapping. There's even an iloc attribute that |
| 68 |
functions similarly to Pandas' DataFrames. Also included is an irange method |
| 69 |
that iterates ranges of keys. |
| 70 |
|
| 71 |
Likewise, sorted sets fully support positional indexing and lookup. |
| 72 |
|
| 73 |
If you're curious how it works, there's a page with implementation details. The |
| 74 |
short answer is two fold: I use bisect a lot and I cheat a little. But never |
| 75 |
fear, none of the graphs hide lazy-computations. |
| 76 |
|
| 77 |
I think :doc:`Sorted Containers<index>` is great. But listen to what other |
| 78 |
smart people say about it: |
| 79 |
|
| 80 |
Alex Martelli says: Good stuff! ... I like the simple, effective implementation |
| 81 |
idea of splitting the sorted containers into smaller “fragments” to avoid the |
| 82 |
O(N) insertion costs. |
| 83 |
|
| 84 |
Jeff Knupp writes: That last part, “fast as C-extensions,” was difficult to |
| 85 |
believe. I would need some sort of performance comparison to be convinced this |
| 86 |
is true. The author includes this in the docs. It is. |
| 87 |
|
| 88 |
Kevin Samuel says: I’m quite amazed, not just by the code quality (it’s |
| 89 |
incredibly readable and has more comment than code, wow), but the actual amount |
| 90 |
of work you put at stuff that is not code: documentation, benchmarking, |
| 91 |
implementation explanations. Even the git log is clean and the unit tests run |
| 92 |
out of the box on Python 2 and 3. |
| 93 |
|
| 94 |
So, the next time you write a priority queue, or work with time-series data, or |
| 95 |
setup an in-memory Sqlite database or send Redis a ZADD command, think of |
| 96 |
:doc:`Sorted Containers<index>`. |
| 97 |
|
| 98 |
In Python, we can do better. Happy Holidays. |
| 99 |
|
| 100 |
.. _`Accompanying Slides`: http://bit.ly/soco5sf |