Faster Python

5,467 views

Published on

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

Published in: Technology

Faster Python

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

×