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

DEFAULTDICT

from collections import defaultdict

data = defaultdict(set)

data[key].add(value)

DICTS OF MUTABLES

if key in data:
    data[key].add(value)
else:
    data[key] = set([value])

Django-1.5.1: django/db/models/sql/query.py

defaultdict vs add_to_dict()

CPython

1.6x

PyPy

1.2x

Jython

0.3x

SETS

Unique values

Unordered

Fast lookup

SETS BEFORE SETS

d = {}
for each in list_of_things:
    d[each] = None

list_of_things = d.keys()

dicts vs lists

Python 2.7

40x

Python 3.3

50x

PyPy 1.9

200x

sets vs dicts

Python 2.7

1.1x

Python 3.3

1.05x

PyPy 1.9

1.06x

SORTING

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

SORTING

retval = set()
for tn in template_names:
    retval.update(search_python(python_code, tn))
retval = list(retval)
retval.sort()
return retval

SORTING

retval = set()
for tn in template_names:
    retval.update(search_python(python_code, tn))
return sorted(retval)

SORTING WITH CMP

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

SORTING WITH CMP

def compare(x, y):
    return cmp(x.modified(), y.modified())

return sorted(catalog_sequence, cmp=compare)

AVERAGE # CALLS

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

SORTING WITH KEY

def get_key(x):
    return x.modified()

return sorted(key=get_key)

AVERAGE # CALLS

len(l)

# calls

Per item

4

4

1

10

10

1

100

100

1

40,000

40,000

1

CONDITIONAL EXPRESSIONS

first_choice = include_blank and blank_choice or []

Django-1.5.1: django/db/models/related.py

CONDITIONAL EXPRESSIONS

first_choice = blank_choice if include_blank else []

CONSTANTS AND LOOPS

const = 5 * 3.5
result = 0
for each in some_iterable:
    result += const

OUTSIDE VS INSIDE

5 * 3.5

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

OUTSIDE VS INSIDE

5 / 3.5

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

result = len(some_iterable) * 17.5

CONSTANTS AND LOOPS

const = 5 * a_var
result = 0
for each in some_iterable:
    result += each * const

OUTSIDE VS INSIDE

each * 5 * a_var

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

OUTSIDE VS INSIDE

each * 5 ** a_var

Python 2.4

1.8x

Python 2.7

2.0x

Python 3.3

2.0x

PyPy 1.9

33x

Jython 2.7

6.4x

STRING CONCATENATION

self._leftover = b''.join([bytes, self._leftover])

Django 1.5.1: django/http/multipartparser.py, Line 355

__add__ vs .join

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

THE MISUNDERSTANDING

This is slow:

result = ''
for text in make_a_lot_of_text():
    result = result + text
return result

THE MISUNDERSTANDING

Much faster:

texts = make_a_lot_of_text()
result = ''.join(texts)
return result

__add__ vs .join

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

MANY COPIES

result = ''
for text in make_a_lot_of_text():
    result = result + text
return result

ONE COPY!

texts = make_a_lot_of_text()
result = ''.join(texts)
return result

THE MISUNDERSTANDING

self._leftover = bytes + self._leftover

THE MISUNDERSTANDING

self._leftover = b''.join([bytes, self._leftover])

Django 1.5.1: django/http/multipartparser.py, Line 355

WHEN TO USE WHAT?

CLOSING CONCATENATION CONCLUSION

THANKS!

SpaceForward
Left, Down, Page DownNext slide
Right, Up, Page UpPrevious slide
POpen presenter console
HToggle this help