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...
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...
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
●
Recur...
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 unuse...
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
...
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 oper...
Slides @
http://www.slideshare.net/atmb4u/faster-python
More Info
http://infiniteloop.in/blog/quick-python-performance-opt...
THANKYOU
on twitter: @atmb4u
Upcoming SlideShare
Loading in...5
×

Faster Python

3,290

Published on

Faster Python Code in 5 minutes. Quick python optimization tips for beginners and advanced users.

Published in: Technology
0 Comments
14 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,290
On Slideshare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
68
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide

Faster Python

  1. 1. Faster Python Code in 5 minutes Anoop Thomas Mathew Profoundis Labs @atmb4u 31st August, 2013 PyCon India 2013
  2. 2. ➢@atmb4u ➢ Co-Founder and CTO @ Profoundis Labs ➢ Avid user of open source since 2003 ➢ Python and Django for last 4 years ABOUTME
  3. 3. Quick Profiling ● %timeit and %prun in ipython ● Try line_profiler ● Not premature optimization TIP1
  4. 4. use Iterators as possible ● Iterators and Generators are memory efficient and scalable ● use itertools as much as possible TIP2
  5. 5. listcomprehension > for loop > while loop consider numpy and tuples for huge data TIP3
  6. 6. xrange instead of range C version of range, designed to work with int TIP4
  7. 7. map is a wonderful idea, so is reduce and filter map(function, data) TIP5
  8. 8. 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
  9. 9. Local variables are faster they are faster than global variables, builtins and attribute lookups TIP7
  10. 10. delete the unnecessary ● use proper namespacing ● weakref is a good idea ● use gc.collect() to expicitly collect the unused variables TIP8
  11. 11. 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
  12. 12. cProfile cStringIO cPickle ... use C versions of library. They can be 100x faster. TIP10
  13. 13. Standard Library & builtin functions are always faster TIP11
  14. 14. use try than if; given mostly True (exeptions exists: builtin functions are always faster) NB: Bad practice! TIP12
  15. 15. immutable types when possible TIP13
  16. 16. ● ''.join('<string>') is faster than + ●insert in a list is O(n) ● Faster lookup in dict, set ●deque for double sided operations TIP14
  17. 17. 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
  18. 18. THANKYOU on twitter: @atmb4u
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×