SlideShare a Scribd company logo
High-Performance Python
Python is fast!
• Python is fast to write, but natively 10x - 100x slower than C.
• Python has great C interop, so you can use C for the slow parts.
• This makes Python competitive with C.
Before you try this at home…
• “Premature optimization is the root of all evil.”
• Use external standards for how fast your code needs to be.
• Remember: performance is a tradeoff against readability, 

maintainability, and developer time.
Part 1:
General Optimization
Profile Your Code
• 95%+ of your code is irrelevant to performance.
• A profiler will tells you which 5% is important.
Profile Your Code
In Python, use cProfile:
source: https://ymichael.com/2014/03/08/profiling-python-with-cprofile.html
Basics
• Make sure your Big-O performance is optimal.
• Move operations outside of loops.
• Use cacheing for repeated calculations.
• Apply algebraic simplifications.
Accidentally Quadratic
The *most* common issue:
def find_intersection(list_one, list_two):
intersection = []
for a in list_one:
if a in list_two:
intersection.append(a)
return intersection
Accidentally Quadratic
The *most* common issue:
def find_intersection(list_one, list_two):
intersection = []
for a in list_one:
if a in list_two:
intersection.append(a)
return intersection
def find_intersection(list_one, list_two):
intersection = []
list_two = set(list_two)
for a in list_one:
if a in list_two:
intersection.append(a)
return intersection
Business Logic
Leverage business logic. You’ll often have 

NP-Complete optimizations to make.
The underlying business reasoning should
guide your approximations.
Part II:
Python Optimization
Libraries
• Use numpy, scipy, pandas, scikit-learn, etc.
• Incredible built-in functionality.



If you need something esoteric, try combining 

built-ins or adapting a more general built-in
approach.
• Extremely fast, thoroughly optimized, and best of all,
already written.
Pure Python Tips
• Function calls are expensive. Avoid them and avoid recursion.
• Check the runtime of built-in data types.
• Make variables local. Global lookups are expensive.
• Use map/filter/reduce instead of for loops, they’re written in C.
• Vectorize! numpy arrays are much faster than lists.
Mixed Tips
• Vectorize! numpy arrays are much faster than lists.
Mixed Tips
def complex_sum(in_list):
in_list = [(a + 2) for a
in in_list]
# more transformations
return sum(in_list)
def complex_sum(in_list):
in_list = np.array(in_list)
in_list += 2
# more transformations
return in_list.sum()
Mixed Tips
• Vectorize! numpy arrays are much faster than lists.
• Array allocation can be a bottleneck. 

Try moving it outside of loops.
Mixed Tips
• Vectorize! numpy arrays are much faster than lists.
• Array allocation can be a bottleneck. 

Try moving it outside of loops.
n = 10 ** 3
output = 0
for i in xrange(10**9):
result = np.zeros(n)
## calculations ##
output += result.sum()
result = np.zeros(10**3)
output = 0
for i in xrange(10**9):
result[:] = 0 # zero out array
## calculations ##
output += result.sum()
• Cython: inline C code directly into Python.
Last Resort: C
def fib(int n):
cdef int a, b, temp
a = 0
b = 1
while b < n:
temp = b
b = a + b
a = temp
• Cython: inline C code directly into Python.
Last Resort: C
def fib(n):
a = 0
b = 1
while b < n:
temp = b
b = a + b
a = temp
return b
• Cython: inline C code directly into Python.
Last Resort: C
def fib(int n):
cdef int a, b, temp
a = 0
b = 1
while b < n:
temp = b
b = a + b
a = temp
return b
Last Resort: C
• Cython: inline C code directly into Python.
• C extensions: write C and call it from Python.
Last Resort: C
• Cython: inline C code directly into Python.
• C extensions: write C and call it from Python.
• Limit these techniques to hot loops.
Things I haven’t mentioned
• multithreading: basically doesn’t work in Python
• pypy: A Python JIT compiler with a different ecosystem
Warning
Optimization is addictive.
Conclusions
• Avoid premature optimizations!

Have objective benchmarks you’re trying to hit.
• Profile your code.

You will be surprised by the results.
• The gold standard for performance is highly-tuned C
(that’s already been written by someone else)
Resources
• Programming Pearls (Jon Bentley)
• accidentallyquadratic.tumblr.com
• Performance Engineering of Software
Systems, 6.172, MIT OpenCourseWare
• cProfile Docs
• Cython Docs
• Guido Van Rossum’s advice:

python.org/doc/essays/list2str
General Python Specific
Contact me: ben@caffeinatedanalytics.com

More Related Content

What's hot (20)

PDF
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
PyData
 
PDF
Reactive Programming
Zhentian Wan
 
PDF
Does reporting takes lots of time
n|u - The Open Security Community
 
PDF
GraphQL With Relay Part Deux
Brad Pillow
 
PPTX
Refactoring Design Patterns the Functional Way (in Scala)
Kfir Bloch
 
PPTX
THE STATE OF OPENTELEMETRY, DOTAN HOROVITS, Logz.io
DevOpsDays Tel Aviv
 
PDF
Nobody Knows What It’s Like To Be the Bad Man: The Development Process for th...
Work-Bench
 
PPTX
Functional programming (Let's fall back in love with Programming)
Sudipta Mukherjee
 
PPTX
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Raffi Khatchadourian
 
PDF
GraphQL & Relay
Viacheslav Slinko
 
PPTX
GraphQL Misconfiguration
Harshit Sengar
 
PPTX
Attacking GraphQL
KavishaSheth1
 
PDF
Introduction to GraphQL
Brainhub
 
PDF
GraphQL IndyJS April 2016
Brad Pillow
 
PPTX
Bug prediction + sdlc automation
Alexey Tokar
 
PDF
Web Applications of the Future with TypeScript and GraphQL
Roy Derks
 
PPTX
Graph ql vs rest
Mihai Enescu
 
PPTX
How to NLProc from .NET
Sergey Tihon
 
PPTX
Deep API Learning (FSE 2016)
Sung Kim
 
PPTX
From Python to smartphones: neural nets @ Saint-Gobain, François Sausset
Pôle Systematic Paris-Region
 
RESTful Machine Learning with Flask and TensorFlow Serving - Carlo Mazzaferro
PyData
 
Reactive Programming
Zhentian Wan
 
Does reporting takes lots of time
n|u - The Open Security Community
 
GraphQL With Relay Part Deux
Brad Pillow
 
Refactoring Design Patterns the Functional Way (in Scala)
Kfir Bloch
 
THE STATE OF OPENTELEMETRY, DOTAN HOROVITS, Logz.io
DevOpsDays Tel Aviv
 
Nobody Knows What It’s Like To Be the Bad Man: The Development Process for th...
Work-Bench
 
Functional programming (Let's fall back in love with Programming)
Sudipta Mukherjee
 
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Raffi Khatchadourian
 
GraphQL & Relay
Viacheslav Slinko
 
GraphQL Misconfiguration
Harshit Sengar
 
Attacking GraphQL
KavishaSheth1
 
Introduction to GraphQL
Brainhub
 
GraphQL IndyJS April 2016
Brad Pillow
 
Bug prediction + sdlc automation
Alexey Tokar
 
Web Applications of the Future with TypeScript and GraphQL
Roy Derks
 
Graph ql vs rest
Mihai Enescu
 
How to NLProc from .NET
Sergey Tihon
 
Deep API Learning (FSE 2016)
Sung Kim
 
From Python to smartphones: neural nets @ Saint-Gobain, François Sausset
Pôle Systematic Paris-Region
 

Viewers also liked (19)

PDF
A Statistician Walks into a Tech Company: R at a Rapidly Scaling Healthcare S...
Work-Bench
 
PDF
Dr. Datascience or: How I Learned to Stop Munging and Love Tests
Work-Bench
 
PDF
Iterating over statistical models: NCAA tournament edition
Work-Bench
 
PDF
Using R at NYT Graphics
Work-Bench
 
PDF
Thinking Small About Big Data
Work-Bench
 
PDF
Julia + R for Data Science
Work-Bench
 
PDF
R for Everything
Work-Bench
 
PDF
Improving Data Interoperability for Python and R
Work-Bench
 
PDF
Building Scalable Prediction Services in R
Work-Bench
 
PDF
I Don't Want to Be a Dummy! Encoding Predictors for Trees
Work-Bench
 
PDF
Reflection on the Data Science Profession in NYC
Work-Bench
 
PDF
The Political Impact of Social Penumbras
Work-Bench
 
PDF
One Algorithm to Rule Them All: How to Automate Statistical Computation
Work-Bench
 
PDF
R Packages for Time-Varying Networks and Extremal Dependence
Work-Bench
 
PDF
Broom: Converting Statistical Models to Tidy Data Frames
Work-Bench
 
PDF
Analyzing NYC Transit Data
Work-Bench
 
PDF
The Feels
Work-Bench
 
PPTX
Inside the R Consortium
Work-Bench
 
PDF
Scaling Data Science at Airbnb
Work-Bench
 
A Statistician Walks into a Tech Company: R at a Rapidly Scaling Healthcare S...
Work-Bench
 
Dr. Datascience or: How I Learned to Stop Munging and Love Tests
Work-Bench
 
Iterating over statistical models: NCAA tournament edition
Work-Bench
 
Using R at NYT Graphics
Work-Bench
 
Thinking Small About Big Data
Work-Bench
 
Julia + R for Data Science
Work-Bench
 
R for Everything
Work-Bench
 
Improving Data Interoperability for Python and R
Work-Bench
 
Building Scalable Prediction Services in R
Work-Bench
 
I Don't Want to Be a Dummy! Encoding Predictors for Trees
Work-Bench
 
Reflection on the Data Science Profession in NYC
Work-Bench
 
The Political Impact of Social Penumbras
Work-Bench
 
One Algorithm to Rule Them All: How to Automate Statistical Computation
Work-Bench
 
R Packages for Time-Varying Networks and Extremal Dependence
Work-Bench
 
Broom: Converting Statistical Models to Tidy Data Frames
Work-Bench
 
Analyzing NYC Transit Data
Work-Bench
 
The Feels
Work-Bench
 
Inside the R Consortium
Work-Bench
 
Scaling Data Science at Airbnb
Work-Bench
 
Ad

Similar to High-Performance Python (20)

PDF
Python高级编程(二)
Qiangning Hong
 
PDF
(Ebook) High Performance Python by Micha Gorelick, Ian Ozsvald
gamvdbw117
 
PDF
High Performance Python 2nd Edition Micha Gorelick
danuzakhiem
 
PDF
High Performance Python 2nd Edition Micha Gorelick Ian Ozsvald
hunelibuzhan
 
PDF
High Performance Python Practical Performant Programming for Humans 2nd Editi...
cemernania2a
 
PPTX
Fast Python: Master the Basics to Write Faster Code
Christopher Conlan
 
PDF
Optimizing Python
AdimianBE
 
PPT
Profiling and optimization
g3_nittala
 
PDF
What’s eating python performance
Piotr Przymus
 
PDF
PyPy's approach to construct domain-specific language runtime
National Cheng Kung University
 
PPT
Euro python2011 High Performance Python
Ian Ozsvald
 
PDF
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
PyData
 
PDF
Python faster for loop
💾 Radek Fabisiak
 
PDF
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
PPTX
Pygrunn 2012 down the rabbit - profiling in python
Remco Wendt
 
PDF
社内勉強会資料_Two Papers Contribute to Faster Python.pdf
NABLAS株式会社
 
PDF
High Performance Python - Marc Garcia
Marc Garcia
 
PPTX
Down the rabbit hole, profiling in Django
Remco Wendt
 
PDF
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
PDF
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
Python高级编程(二)
Qiangning Hong
 
(Ebook) High Performance Python by Micha Gorelick, Ian Ozsvald
gamvdbw117
 
High Performance Python 2nd Edition Micha Gorelick
danuzakhiem
 
High Performance Python 2nd Edition Micha Gorelick Ian Ozsvald
hunelibuzhan
 
High Performance Python Practical Performant Programming for Humans 2nd Editi...
cemernania2a
 
Fast Python: Master the Basics to Write Faster Code
Christopher Conlan
 
Optimizing Python
AdimianBE
 
Profiling and optimization
g3_nittala
 
What’s eating python performance
Piotr Przymus
 
PyPy's approach to construct domain-specific language runtime
National Cheng Kung University
 
Euro python2011 High Performance Python
Ian Ozsvald
 
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
PyData
 
Python faster for loop
💾 Radek Fabisiak
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Pygrunn 2012 down the rabbit - profiling in python
Remco Wendt
 
社内勉強会資料_Two Papers Contribute to Faster Python.pdf
NABLAS株式会社
 
High Performance Python - Marc Garcia
Marc Garcia
 
Down the rabbit hole, profiling in Django
Remco Wendt
 
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
Ad

More from Work-Bench (8)

PDF
2017 Enterprise Almanac
Work-Bench
 
PDF
AI to Enable Next Generation of People Managers
Work-Bench
 
PDF
Startup Recruiting Workbook: Sourcing and Interview Process
Work-Bench
 
PDF
Cloud Native Infrastructure Management Solutions Compared
Work-Bench
 
PPTX
Building a Demand Generation Machine at MongoDB
Work-Bench
 
PPTX
How to Market Your Startup to the Enterprise
Work-Bench
 
PDF
Marketing & Design for the Enterprise
Work-Bench
 
PDF
Playing the Marketing Long Game
Work-Bench
 
2017 Enterprise Almanac
Work-Bench
 
AI to Enable Next Generation of People Managers
Work-Bench
 
Startup Recruiting Workbook: Sourcing and Interview Process
Work-Bench
 
Cloud Native Infrastructure Management Solutions Compared
Work-Bench
 
Building a Demand Generation Machine at MongoDB
Work-Bench
 
How to Market Your Startup to the Enterprise
Work-Bench
 
Marketing & Design for the Enterprise
Work-Bench
 
Playing the Marketing Long Game
Work-Bench
 

Recently uploaded (20)

PPTX
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
DOCX
COT Feb 19, 2025 DLLgvbbnnjjjjjj_Digestive System and its Functions_PISA_CBA....
kayemorales1105
 
PPTX
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
PPTX
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
PDF
Datàaaaaaaaaaengineeeeeeeeeeeeeeeeeeeeeee
juadsr96
 
PPTX
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
PPTX
Project_Update_Summary.for the use from PM
Odysseas Lekatsas
 
PDF
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
DOCX
ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMEN...
JoemarAgbayani1
 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
 
PDF
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
PPTX
Krezentios memories in college data.pptx
notknown9
 
PDF
Exploiting the Low Volatility Anomaly: A Low Beta Model Portfolio for Risk-Ad...
Bradley Norbom, CFA
 
PDF
Blood pressure (3).pdfbdbsbsbhshshshhdhdhshshs
hernandezemma379
 
PDF
UNISE-Operation-Procedure-InDHIS2trainng
ahmedabduselam23
 
PDF
TCU EVALUATION FACULTY TCU Taguig City 1st Semester 2017-2018
MELJUN CORTES
 
PPTX
Monitoring Improvement ( Pomalaa Branch).pptx
fajarkunee
 
PPTX
Data anlytics Hospitals Research India.pptx
SayantanChakravorty2
 
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
COT Feb 19, 2025 DLLgvbbnnjjjjjj_Digestive System and its Functions_PISA_CBA....
kayemorales1105
 
美国史蒂文斯理工学院毕业证书{SIT学费发票SIT录取通知书}哪里购买
Taqyea
 
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
Datàaaaaaaaaaengineeeeeeeeeeeeeeeeeeeeeee
juadsr96
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
Project_Update_Summary.for the use from PM
Odysseas Lekatsas
 
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMENT AS OF MAY 15 RCT ACCOMPLISHMEN...
JoemarAgbayani1
 
How to Add Columns and Rows in an R Data Frame
subhashenia
 
Orchestrating Data Workloads With Airflow.pdf
ssuserae5511
 
Krezentios memories in college data.pptx
notknown9
 
Exploiting the Low Volatility Anomaly: A Low Beta Model Portfolio for Risk-Ad...
Bradley Norbom, CFA
 
Blood pressure (3).pdfbdbsbsbhshshshhdhdhshshs
hernandezemma379
 
UNISE-Operation-Procedure-InDHIS2trainng
ahmedabduselam23
 
TCU EVALUATION FACULTY TCU Taguig City 1st Semester 2017-2018
MELJUN CORTES
 
Monitoring Improvement ( Pomalaa Branch).pptx
fajarkunee
 
Data anlytics Hospitals Research India.pptx
SayantanChakravorty2
 

High-Performance Python

  • 2. Python is fast! • Python is fast to write, but natively 10x - 100x slower than C. • Python has great C interop, so you can use C for the slow parts. • This makes Python competitive with C.
  • 3. Before you try this at home… • “Premature optimization is the root of all evil.” • Use external standards for how fast your code needs to be. • Remember: performance is a tradeoff against readability, 
 maintainability, and developer time.
  • 5. Profile Your Code • 95%+ of your code is irrelevant to performance. • A profiler will tells you which 5% is important.
  • 6. Profile Your Code In Python, use cProfile: source: https://ymichael.com/2014/03/08/profiling-python-with-cprofile.html
  • 7. Basics • Make sure your Big-O performance is optimal. • Move operations outside of loops. • Use cacheing for repeated calculations. • Apply algebraic simplifications.
  • 8. Accidentally Quadratic The *most* common issue: def find_intersection(list_one, list_two): intersection = [] for a in list_one: if a in list_two: intersection.append(a) return intersection
  • 9. Accidentally Quadratic The *most* common issue: def find_intersection(list_one, list_two): intersection = [] for a in list_one: if a in list_two: intersection.append(a) return intersection def find_intersection(list_one, list_two): intersection = [] list_two = set(list_two) for a in list_one: if a in list_two: intersection.append(a) return intersection
  • 10. Business Logic Leverage business logic. You’ll often have 
 NP-Complete optimizations to make. The underlying business reasoning should guide your approximations.
  • 12. Libraries • Use numpy, scipy, pandas, scikit-learn, etc. • Incredible built-in functionality.
 
 If you need something esoteric, try combining 
 built-ins or adapting a more general built-in approach. • Extremely fast, thoroughly optimized, and best of all, already written.
  • 13. Pure Python Tips • Function calls are expensive. Avoid them and avoid recursion. • Check the runtime of built-in data types. • Make variables local. Global lookups are expensive. • Use map/filter/reduce instead of for loops, they’re written in C.
  • 14. • Vectorize! numpy arrays are much faster than lists. Mixed Tips
  • 15. • Vectorize! numpy arrays are much faster than lists. Mixed Tips def complex_sum(in_list): in_list = [(a + 2) for a in in_list] # more transformations return sum(in_list) def complex_sum(in_list): in_list = np.array(in_list) in_list += 2 # more transformations return in_list.sum()
  • 16. Mixed Tips • Vectorize! numpy arrays are much faster than lists. • Array allocation can be a bottleneck. 
 Try moving it outside of loops.
  • 17. Mixed Tips • Vectorize! numpy arrays are much faster than lists. • Array allocation can be a bottleneck. 
 Try moving it outside of loops. n = 10 ** 3 output = 0 for i in xrange(10**9): result = np.zeros(n) ## calculations ## output += result.sum() result = np.zeros(10**3) output = 0 for i in xrange(10**9): result[:] = 0 # zero out array ## calculations ## output += result.sum()
  • 18. • Cython: inline C code directly into Python. Last Resort: C
  • 19. def fib(int n): cdef int a, b, temp a = 0 b = 1 while b < n: temp = b b = a + b a = temp • Cython: inline C code directly into Python. Last Resort: C def fib(n): a = 0 b = 1 while b < n: temp = b b = a + b a = temp return b
  • 20. • Cython: inline C code directly into Python. Last Resort: C def fib(int n): cdef int a, b, temp a = 0 b = 1 while b < n: temp = b b = a + b a = temp return b
  • 21. Last Resort: C • Cython: inline C code directly into Python. • C extensions: write C and call it from Python.
  • 22. Last Resort: C • Cython: inline C code directly into Python. • C extensions: write C and call it from Python. • Limit these techniques to hot loops.
  • 23. Things I haven’t mentioned • multithreading: basically doesn’t work in Python • pypy: A Python JIT compiler with a different ecosystem
  • 25. Conclusions • Avoid premature optimizations!
 Have objective benchmarks you’re trying to hit. • Profile your code.
 You will be surprised by the results. • The gold standard for performance is highly-tuned C (that’s already been written by someone else)
  • 26. Resources • Programming Pearls (Jon Bentley) • accidentallyquadratic.tumblr.com • Performance Engineering of Software Systems, 6.172, MIT OpenCourseWare • cProfile Docs • Cython Docs • Guido Van Rossum’s advice:
 python.org/doc/essays/list2str General Python Specific Contact me: [email protected]