PABLO FANQUE CIRCUS ROYAL,
WARSAW, POLAND
DJANGOCON EU BONANZA
AND POSITIVELY THE
LAST PRESENTATION
not counting the lightning talks
BEING FOR THE
BENEFIT OF MR PONY
PREHISTORIC
PATTERNS
IN PYTHON
LENNART REGEBRO, ESQ.
THE CELEBRATED PYTHON HUGGER!
PLONE DANCER, VAULTER, RIDER ETC.
Grandest Night of the Season!
On Afternoon 16:05, Friday 17th of May, 2013
from collections import defaultdict data = defaultdict(set) data[key].add(value)
if key in data: data[key].add(value) else: data[key] = set([value])
CPython | 1.6x |
PyPy | 1.2x |
Jython | 0.3x |
Unique values
Unordered
Fast lookup
d = {} for each in list_of_things: d[each] = None list_of_things = d.keys()
Python 2.7 | 40x |
Python 3.3 | 50x |
PyPy 1.9 | 200x |
Python 2.7 | 1.1x |
Python 3.3 | 1.05x |
PyPy 1.9 | 1.06x |
Prehistoric code:
retval = [] for tn in template_names: retval.extend(search_python(python_code, tn)) retval = list(set(retval)) retval.sort() return retval
Django 1.5.1: extras/csrf_migration_helper.py
retval = set() for tn in template_names: retval.update(search_python(python_code, tn)) retval = list(retval) retval.sort() return retval
retval = set() for tn in template_names: retval.update(search_python(python_code, tn)) return sorted(retval)
sorted = catalog_sequence[:] sorted.sort(lambda x, y: cmp(x.modified(), y.modified())) return sorted
Plone 4.0: Products/CMFPlone/skins/plone_scripts/sort_modified_ascending.py
def compare(x, y): return cmp(x.modified(), y.modified()) return sorted(catalog_sequence, cmp=compare)
len(l) | # calls | Per item |
4 | 6 | 1.5 |
10 | 22 | 2.2 |
100 | 528 | 5.28 |
40,000 | 342,541 | 8.56 |
Reference: Jarret Hardie in Python Magazine
def get_key(x): return x.modified() return sorted(key=get_key)
len(l) | # calls | Per item |
4 | 4 | 1 |
10 | 10 | 1 |
100 | 100 | 1 |
40,000 | 40,000 | 1 |
first_choice = include_blank and blank_choice or []
Django-1.5.1: django/db/models/related.py
first_choice = blank_choice if include_blank else []
const = 5 * 3.5 result = 0 for each in some_iterable: result += const
Python 2.4 | 2.0x |
Python 2.7 | 1.0x |
Python 3.3 | 1.0x |
PyPy 1.9 | 1.0x |
Jython 2.7 | 1.2x |
Python 2.4 | 2.0x |
Python 2.7 | 2.0x |
Python 3.3 | 1.0x |
PyPy 1.9 | 1.0x |
Jython 2.7 | 1.2x |
const = 5 * a_var result = 0 for each in some_iterable: result += each * const
Python 2.4 | 1.3x |
Python 2.7 | 1.3x |
Python 3.3 | 1.3x |
PyPy 1.9 | 1.0x |
Jython 2.7 | 1.7x |
Python 2.4 | 1.8x |
Python 2.7 | 2.0x |
Python 3.3 | 2.0x |
PyPy 1.9 | 33x |
Jython 2.7 | 6.4x |
self._leftover = b''.join([bytes, self._leftover])
Django 1.5.1: django/http/multipartparser.py, Line 355
Python 2.4 | 1.5x |
Python 2.7 | 1.4x |
Python 3.3 | 1.3x |
PyPy 1.9 | 1.0x |
Jython 2.7 | 1.8x |
This is slow:
result = '' for text in make_a_lot_of_text(): result = result + text return result
Much faster:
texts = make_a_lot_of_text() result = ''.join(texts) return result
Python 2.4 | 0.5x |
Python 2.7 | 0.5x |
Python 3.3 | 0.5x |
PyPy 1.9 | 1.0x |
Jython 2.7 | 0.004x |
result = '' for text in make_a_lot_of_text(): result = result + text return result
texts = make_a_lot_of_text() result = ''.join(texts) return result
self._leftover = bytes + self._leftover
self._leftover = b''.join([bytes, self._leftover])
Django 1.5.1: django/http/multipartparser.py, Line 355
Space | Forward |
---|---|
Left, Down, Page Down | Next slide |
Right, Up, Page Up | Previous slide |
P | Open presenter console |
H | Toggle this help |