Performance Improvements with Cython
Kaivalya Rawal
Singapore Python User Group
October 2018 Meetup
What this talk is about
Singapore Python User Group, October 2018 Meetup
What this talk is not about
Singapore Python User Group, October 2018 Meetup
Live Demonstration 1
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
Generating the Nth fibonacci number: O(n)
Generating the first N fibonacci numbers: O(n) O(n2)
Singapore Python User Group, October 2018 Meetup
Live Demonstration 1
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
Generating the Nth fibonacci number: O(n)
Generating the first N fibonacci numbers: O(n) O(n2) for “slow version”
Demo: Speeding up “Slow Fibonacci” with Cython, to find the 34th Fibonacci number
Singapore Python User Group, October 2018 Meetup
Programming Languages
A human-computer contract, ie a set of instructions a human can write for a computer to perform.
Singapore Python User Group, October 2018 Meetup
Programming Languages
A human-computer contract, ie a set of instructions a human can write for a computer to perform.
(Important?) Variations:
Singapore Python User Group, October 2018 Meetup
Programming Languages
Singapore Python User Group, October 2018 Meetup
2018: Programming for Humans
Automatic Memory Allocation
Simple Syntax
Relatively Higher Level
Language Features
Lower Speeds
Contract is
“Human Friendly”
Manual Memory Allocation
Terse Syntax
Lower Level Control
High Speed
Contract is
“Computer Friendly”
Singapore Python User Group, October 2018 Meetup
contracts are increasingly human-human too
2018: Programming for Humans
Automatic Memory Allocation
Simple Syntax
Relatively Higher Level
Language Features
Lower Speeds
Contract is
“Human Friendly”
Manual Memory Allocation
Terse Syntax
Lower Level Control
High Speed
Contract is
“Computer Friendly”
Singapore Python User Group, October 2018 Meetup
strike a balance between human and computer friendly
Cython
Cython
A different implementation of the same contract (as CPython).
Cython ≠ CPython
A superset of Python.
Singapore Python User Group, October 2018 Meetup
Cython
Python* Code
↓
Cython Generated ‘C’
↓
Program Output
A different implementation of the same contract (as CPython).
Cython ≠ CPython
A superset of Python.
Singapore Python User Group, October 2018 Meetup
Live Demonstration 2
Python* Code
↓
Cython Generated ‘C’
↓
Program Output
Fibonacci again
Extra steps:
Singapore Python User Group, October 2018 Meetup
Cython - Overview
Examples
Singapore Python User Group, October 2018 Meetup
Cython - Overview
Concepts
Examples
Singapore Python User Group, October 2018 Meetup
Why use Cython?
Singapore Python User Group, October 2018 Meetup
Speed
Simplicity
C
Python
Cython
Cython - Optimising Large Codebases
Certainly possible: scikit-learn, scipy, pandas, pracmln, etc already use it in production
Still at version 0.30, not a complete* superset yet! But still Turing Complete.
Singapore Python User Group, October 2018 Meetup
Cython - Optimising Large Codebases
Certainly possible: scikit-learn, scipy, pandas, pracmln, etc already use it in production
Still at version 0.30, not a complete* superset yet! But still Turing Complete.
Two techniques to help “cythonize” existing code:
Singapore Python User Group, October 2018 Meetup
Live Demonstration 3
Annotations
Singapore Python User Group, October 2018 Meetup
Annotations vs Profiling
“Python Interaction” is not a reliable metric. (80-20 rule)
Lead developer might know better than the annotation tool which parts of the code deserve optimisation.
Singapore Python User Group, October 2018 Meetup
Annotations vs Profiling
“Python Interaction” is not a reliable metric. (80-20 rule)
Lead developer might know better than the annotation tool which parts of the code deserve optimisation.
“Premature optimization is the root of all evil” - Donald Knuth
“Never optimize without having profiled. Let me repeat this: Never optimize without having profiled your code. Your thoughts about which part of your code takes too much time are wrong.”
Singapore Python User Group, October 2018 Meetup
Live Demonstration 4
Profiling
Use built-in cProfile profiler
Extra steps:
Singapore Python User Group, October 2018 Meetup
Getting Cython Help
Singapore Python User Group, October 2018 Meetup
Getting Cython Help
Singapore Python User Group, October 2018 Meetup
Getting Cython Help
Singapore Python User Group, October 2018 Meetup
Getting Cython Help
Singapore Python User Group, October 2018 Meetup
Thank You
Questions?