Performance

Notes taken during a Performance talk given at PyConUK.

function calls

c method instead
cython instead

Use operators

Loops

Avoid

list comprehension is quicker than for loop over an emnumerator

What is op?

xrange vs range
– no wasted memory

NB: In python3 renamed range

itertools

Or use a c extension

mstexttools

Sorting

l.sort vs sorted() – any iterator

Key based:

look up operator -> operator.itemgetter

Can use on object attribute

Strings:

“”.join([a, b, c]) faster but less readable

% method not that fast an ugly – don’t use

Dictionary
Int keys are faster than string keys

interned string keys – no garbage collection

sys.intern – P3

Variable look ups

Mentioned global variable trick

local var faster than global in general

Look up: Old style/new style classes??

Slot attributes ?? look up more just seemed to be attributes???

Don’t use exceptions as an often use case. => much faster say to use getattr than catch a valueexception

Try to rely on c methods

Profiling tools:
c profiler built into Python

cpython vs number – can make pythonrun as fast as C – suggestion