KEMBAR78
Faster Python | PDF
Faster
Python Code
in 5 minutes
Anoop Thomas Mathew
Profoundis Labs
@atmb4u
31st August, 2013
PyCon India 2013
➢@atmb4u
➢ Co-Founder and CTO @ Profoundis Labs
➢ Avid user of open source since 2003
➢ Python and Django for last 4 years
ABOUTME
Quick Profiling
●
%timeit and %prun in ipython
●
Try line_profiler
●
Not premature optimization
TIP1
use Iterators as possible
●
Iterators and Generators are memory efficient and
scalable
●
use itertools as much as possible
TIP2
listcomprehension >
for loop >
while loop
consider numpy and tuples for huge data
TIP3
xrange instead of range
C version of range, designed to work with int
TIP4
map is a wonderful idea,
so is reduce and filter
map(function, data)
TIP5
Function calls are costly
●
pass entire list to a function than iterating on each
element and calling the function
●
Recursive loops are dangerous with quadratic
complexity
TIP6
Local variables are faster
they are faster than global variables, builtins and
attribute lookups
TIP7
delete the unnecessary
●
use proper namespacing
●
weakref is a good idea
●
use gc.collect() to expicitly collect the unused variables
TIP8
Threads for I/O
ctypes for CPU(GIL)
●
use threads for I/O bound processes.
●
For CPU bound with GIL lock, use gevent or C
extensions to override it
TIP9
cProfile
cStringIO
cPickle ...
use C versions of library. They can be 100x faster.
TIP10
Standard Library
&
builtin functions
are always faster
TIP11
use try than if; given mostly True
(exeptions exists: builtin functions are always faster)
NB: Bad practice!
TIP12
immutable types when possible
TIP13
●
''.join('<string>') is faster than +
●insert in a list is O(n)
●
Faster lookup in dict, set
●deque for double sided operations
TIP14
Slides @
http://www.slideshare.net/atmb4u/faster-python
More Info
http://infiniteloop.in/blog/quick-python-performance-optimization-part-i/
http://infiniteloop.in/blog/quick-python-performance-optimization-part-ii/
http://wiki.python.org/moin/PythonSpeed/PerformanceTips
http://wiki.python.org/moin/PythonSpeed
MOREINFO
THANKYOU
on twitter: @atmb4u

Faster Python