SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Pythonic doesn't mean slow!
Ronan Lamy
About meAbout me
PyPy core dev
Python consultant and freelance developer
Contact:
Ronan.Lamy@gmail.com
@ronanlamy
We love PythonWe love Python
Why Python?Why Python?
Easy to learn
Easy to read
Simplicity
Community
Many libraries
Technical reasonsTechnical reasons
"Readability counts"
Uniform object model
Simple & expressive APIs
Powerful abstractions
Modularity/extensibility
What about performance?What about performance?
Who cares about performance?Who cares about performance?
Workaround #1: Using high-performance librariesWorkaround #1: Using high-performance libraries
GoodGood
Feels like Python
Also includes domain knowledge
Actually the default option
BadBad
Somewhat slow
Limited customisation options
Can only use what's implemented
Workaround #2: Rewrite in CWorkaround #2: Rewrite in C
Cython
C + cf
Write a C extension 😱
Numba
...
GoodGood
Fast
Developer has full control
BadBad
Different, less convenient, language
Unoptimised parts can become signi cant again
Python/C boundary is a bottleneck
C implementations cause bad APIsC implementations cause bad APIs
In [ ]: import numpy
class myarray(numpy.ndarray):
pass
In [ ]: import pandas
help(pandas.read_csv)
Fast vs PythonicFast vs Pythonic
PyPyPyPy
Fast and compliant implementation of Python
PyPy v7.2 released on 14 October
Supports 2.7 and 3.6
JIT compiler
GC tuned for Python
PyPy - DemoPyPy - Demo
In [2]: %%time
class Quantity:
def __init__(self, value, unit):
self.value = value
self.unit = unit
def __add__(self, other):
if isinstance(other, Quantity):
if other.unit != self.unit: raise ValueError("units must match")
else: return Quantity(self.value + other.value, self.unit)
else: return NotImplemented
def __str__(self):
return f"{self.value} {self.unit}"
def compute(n):
total = Quantity(0, 'm')
increment = Quantity(1., 'm')
for i in range(n):
total += increment
return total
N = 1_000_000_000
print(compute(N))
3.6.9 (default, Jul 3 2019, 15:36:16)
[GCC 5.4.0 20160609]
1000000000.0 m
CPU times: user 29min 4s, sys: 112 ms, total: 29min 4s
Wall time: 29min 4s
How fast is PyPy?How fast is PyPy?
It dependsIt depends
Up to 1000x faster than CPython in extreme cases
Up to 50x in more realistic cases
2-5x on "typical" applications
Can be slower (short scripts, tests, ...)
C extension supportC extension support
cpyext = "Python.h for PyPy"
numpy, scipy, pandas, scikit-learn, lxml, ...
Cython + most extensions written in Cython
'pip install' works
Wheels available at
https://github.com/antocuni/pypy-wheels
PyPy qualitiesPyPy qualities
Make Pythonic code fast
Abstractions for free
Make Python a 1st-class language for HPC
PyPy issuesPyPy issues
Lags behind CPython
Implementation details are different
Need to understand GC and JIT to get best performance
No conda
Small community
cpyext issuescpyext issues
CPython API is huge
Grey areas
Patchy library support
High overhead
HPyHPy
New C API
No refcounting
Replace PyObject* with opaque handles
Don't expose implementation details
https://github.com/pyhandle/hpy (https://github.com/pyhandle/hpy)
HPy: plansHPy: plans
In one yearIn one year
CPython: implemented on top of existing API
PyPy: much more ef cient than cpyext
Used by Cython
Long-termLong-term
Replaces current C-API
Used by all relevant implementations
CPython free to experiment and modify internals
SummarySummary
PyPy makes pure-Python fast
Compatible with PyData ecosystem
Computing in Python becomes viable
Complements traditional approaches to performance
ContactContact
IRC: #pypy on Freenode IRC
pypy-dev @ python.org
Questions?
http://pypy.org (http://pypy.org)
The endThe end

Más contenido relacionado

La actualidad más candente

Mp24: Python in gaming industry
Mp24: Python in gaming industryMp24: Python in gaming industry
Mp24: Python in gaming industry
Montreal Python
 

La actualidad más candente (20)

UNSW Australia Robocup Recap Hefei 2015
UNSW Australia Robocup Recap Hefei 2015UNSW Australia Robocup Recap Hefei 2015
UNSW Australia Robocup Recap Hefei 2015
 
Raspberry using Python Session 1
Raspberry using Python Session 1Raspberry using Python Session 1
Raspberry using Python Session 1
 
Top 10 python ide
Top 10 python ideTop 10 python ide
Top 10 python ide
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming
 
Py Con 2009 Pumping Iron Into Python
Py Con 2009   Pumping Iron Into PythonPy Con 2009   Pumping Iron Into Python
Py Con 2009 Pumping Iron Into Python
 
Getting started with Linux and Python by Caffe
Getting started with Linux and Python by CaffeGetting started with Linux and Python by Caffe
Getting started with Linux and Python by Caffe
 
First python project
First python projectFirst python project
First python project
 
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
Raspberry using Python Session 3
Raspberry using Python Session 3Raspberry using Python Session 3
Raspberry using Python Session 3
 
gRPC 프레임워크를 만들며 알아보는 파이썬 - 파이콘2020
gRPC 프레임워크를 만들며 알아보는 파이썬  - 파이콘2020gRPC 프레임워크를 만들며 알아보는 파이썬  - 파이콘2020
gRPC 프레임워크를 만들며 알아보는 파이썬 - 파이콘2020
 
Mp24: Python in gaming industry
Mp24: Python in gaming industryMp24: Python in gaming industry
Mp24: Python in gaming industry
 
平行化你的工作 part1
平行化你的工作 part1平行化你的工作 part1
平行化你的工作 part1
 
Porting To Symbian
Porting To SymbianPorting To Symbian
Porting To Symbian
 
Machine Learning on Your Hand - Introduction to Tensorflow Lite Preview
Machine Learning on Your Hand - Introduction to Tensorflow Lite PreviewMachine Learning on Your Hand - Introduction to Tensorflow Lite Preview
Machine Learning on Your Hand - Introduction to Tensorflow Lite Preview
 
Number of Computer Languages = 3
Number of Computer Languages = 3Number of Computer Languages = 3
Number of Computer Languages = 3
 
PyPy London Demo Evening 2013
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
Top python interview question and answer
Top python interview question and answerTop python interview question and answer
Top python interview question and answer
 

Similar a Pythonic doesn't mean slow!

Similar a Pythonic doesn't mean slow! (20)

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Why learn python in 2017?
Why learn python in 2017?Why learn python in 2017?
Why learn python in 2017?
 
Introduction To Python
Introduction To PythonIntroduction To Python
Introduction To Python
 
Introduction to Python Programming Basics
Introduction  to  Python  Programming BasicsIntroduction  to  Python  Programming Basics
Introduction to Python Programming Basics
 
Python 101 For The Net Developer
Python 101 For The Net DeveloperPython 101 For The Net Developer
Python 101 For The Net Developer
 
Py4 inf 01-intro
Py4 inf 01-introPy4 inf 01-intro
Py4 inf 01-intro
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
 
Python
PythonPython
Python
 
Python Programming Draft PPT.pptx
Python Programming Draft PPT.pptxPython Programming Draft PPT.pptx
Python Programming Draft PPT.pptx
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
Research paper on python by Rj
Research paper on python by RjResearch paper on python by Rj
Research paper on python by Rj
 
POWER OF PYTHON PROGRAMMING LANGUAGE
POWER OF PYTHON PROGRAMMING LANGUAGE POWER OF PYTHON PROGRAMMING LANGUAGE
POWER OF PYTHON PROGRAMMING LANGUAGE
 
Cython compiler
Cython compilerCython compiler
Cython compiler
 
Hello World! with Python
Hello World! with PythonHello World! with Python
Hello World! with Python
 
IRJET- Python: Simple though an Important Programming Language
IRJET- Python: Simple though an Important Programming LanguageIRJET- Python: Simple though an Important Programming Language
IRJET- Python: Simple though an Important Programming Language
 
Comparison between python and c++
Comparison between python and c++Comparison between python and c++
Comparison between python and c++
 
Learn Python in Pragmatic Way
Learn Python in Pragmatic WayLearn Python in Pragmatic Way
Learn Python in Pragmatic Way
 

Último

Último (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

Pythonic doesn't mean slow!

  • 1. Pythonic doesn't mean slow! Ronan Lamy
  • 2. About meAbout me PyPy core dev Python consultant and freelance developer Contact: Ronan.Lamy@gmail.com @ronanlamy
  • 3. We love PythonWe love Python
  • 4. Why Python?Why Python? Easy to learn Easy to read Simplicity Community Many libraries
  • 5. Technical reasonsTechnical reasons "Readability counts" Uniform object model Simple & expressive APIs Powerful abstractions Modularity/extensibility
  • 6. What about performance?What about performance? Who cares about performance?Who cares about performance?
  • 7. Workaround #1: Using high-performance librariesWorkaround #1: Using high-performance libraries
  • 8. GoodGood Feels like Python Also includes domain knowledge Actually the default option BadBad Somewhat slow Limited customisation options Can only use what's implemented
  • 9. Workaround #2: Rewrite in CWorkaround #2: Rewrite in C Cython C + cf Write a C extension 😱 Numba ...
  • 10. GoodGood Fast Developer has full control BadBad Different, less convenient, language Unoptimised parts can become signi cant again Python/C boundary is a bottleneck
  • 11. C implementations cause bad APIsC implementations cause bad APIs In [ ]: import numpy class myarray(numpy.ndarray): pass In [ ]: import pandas help(pandas.read_csv)
  • 12. Fast vs PythonicFast vs Pythonic
  • 13. PyPyPyPy Fast and compliant implementation of Python PyPy v7.2 released on 14 October Supports 2.7 and 3.6 JIT compiler GC tuned for Python
  • 14. PyPy - DemoPyPy - Demo In [2]: %%time class Quantity: def __init__(self, value, unit): self.value = value self.unit = unit def __add__(self, other): if isinstance(other, Quantity): if other.unit != self.unit: raise ValueError("units must match") else: return Quantity(self.value + other.value, self.unit) else: return NotImplemented def __str__(self): return f"{self.value} {self.unit}" def compute(n): total = Quantity(0, 'm') increment = Quantity(1., 'm') for i in range(n): total += increment return total N = 1_000_000_000 print(compute(N)) 3.6.9 (default, Jul 3 2019, 15:36:16) [GCC 5.4.0 20160609] 1000000000.0 m CPU times: user 29min 4s, sys: 112 ms, total: 29min 4s Wall time: 29min 4s
  • 15. How fast is PyPy?How fast is PyPy? It dependsIt depends Up to 1000x faster than CPython in extreme cases Up to 50x in more realistic cases 2-5x on "typical" applications Can be slower (short scripts, tests, ...)
  • 16. C extension supportC extension support cpyext = "Python.h for PyPy" numpy, scipy, pandas, scikit-learn, lxml, ... Cython + most extensions written in Cython 'pip install' works Wheels available at https://github.com/antocuni/pypy-wheels
  • 17. PyPy qualitiesPyPy qualities Make Pythonic code fast Abstractions for free Make Python a 1st-class language for HPC
  • 18. PyPy issuesPyPy issues Lags behind CPython Implementation details are different Need to understand GC and JIT to get best performance No conda Small community
  • 19. cpyext issuescpyext issues CPython API is huge Grey areas Patchy library support High overhead
  • 20. HPyHPy New C API No refcounting Replace PyObject* with opaque handles Don't expose implementation details https://github.com/pyhandle/hpy (https://github.com/pyhandle/hpy)
  • 21. HPy: plansHPy: plans In one yearIn one year CPython: implemented on top of existing API PyPy: much more ef cient than cpyext Used by Cython Long-termLong-term Replaces current C-API Used by all relevant implementations CPython free to experiment and modify internals
  • 22. SummarySummary PyPy makes pure-Python fast Compatible with PyData ecosystem Computing in Python becomes viable Complements traditional approaches to performance
  • 23. ContactContact IRC: #pypy on Freenode IRC pypy-dev @ python.org Questions? http://pypy.org (http://pypy.org)