1 of 28

Performance Improvements with Cython

Kaivalya Rawal

Singapore Python User Group

October 2018 Meetup

2 of 28

What this talk is about

  • A very brief introduction to Cython, from an amateur enthusiast’s perspective
  • Motivation and reasons to use Cython, based on my recent work experience with The Institute for Artificial Intelligence, University of Bremen.
  • Tips on Cython optimisation and troubleshooting when working with large codebases

Singapore Python User Group, October 2018 Meetup

3 of 28

What this talk is not about

  • Providing a thorough outline of Cython capabilities
  • Describing best practices for Cython development
  • Expert insights from experienced Cython developers

Singapore Python User Group, October 2018 Meetup

4 of 28

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

5 of 28

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

6 of 28

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

7 of 28

Programming Languages

A human-computer contract, ie a set of instructions a human can write for a computer to perform.

(Important?) Variations:

  • Compiled vs Interpreted;
  • Object Oriented vs Functional;
  • Static vs Dynamic;
  • ...

Singapore Python User Group, October 2018 Meetup

8 of 28

Programming Languages

Singapore Python User Group, October 2018 Meetup

9 of 28

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

10 of 28

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

11 of 28

Cython

A different implementation of the same contract (as CPython).

Cython ≠ CPython

A superset of Python.

Singapore Python User Group, October 2018 Meetup

12 of 28

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

13 of 28

Live Demonstration 2

Python* Code

Cython Generated ‘C’

Program Output

Fibonacci again

Extra steps:

  1. Change extension to .pyx
  2. Create setup.py
  3. Compile with build_ext --inplace

Singapore Python User Group, October 2018 Meetup

14 of 28

Cython - Overview

Examples

  • nint n
  • cchar c
  • ddict d
  • def fibcpdef fib
  • def fibcdef fib
  • class Ccdef class C
    1. Create .pxd file
  • with NoGIL
  • Direct interfacing with C structs / procedures

Singapore Python User Group, October 2018 Meetup

15 of 28

Cython - Overview

Concepts

  1. Static typing (variable)
  2. Static typing (variable)
  3. Static typing (variable)
  4. Static typing (params and return)
  5. Static typing (C only)
  6. Static typing (attributes)
  7. ?
  8. ?

Examples

  • nint n
  • cchar c
  • ddict d
  • def fibcpdef fib
  • def fibcdef fib
  • class Ccdef class C
    • Create .pxd file
  • with NoGIL
  • Direct interfacing with C structs / procedures

Singapore Python User Group, October 2018 Meetup

16 of 28

Why use Cython?

  • Customizable balance between Speed and Simplicity
  • Incrementally speed up existing code, without the need for major redesigns*
  • Generation of potentially higher quality C
  • Faster development times

Singapore Python User Group, October 2018 Meetup

Speed

Simplicity

C

Python

Cython

17 of 28

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

18 of 28

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:

  • Annotation
  • Profiling

Singapore Python User Group, October 2018 Meetup

19 of 28

Live Demonstration 3

Annotations

  • In a Jupyter Notebook
  • In standalone files

Singapore Python User Group, October 2018 Meetup

20 of 28

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

21 of 28

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

22 of 28

Live Demonstration 4

Profiling

Use built-in cProfile profiler

Extra steps:

  1. Add compiler directive
  2. External analyser - cprofilev / snakeviz

Singapore Python User Group, October 2018 Meetup

23 of 28

Getting Cython Help

Singapore Python User Group, October 2018 Meetup

24 of 28

Getting Cython Help

Singapore Python User Group, October 2018 Meetup

25 of 28

Getting Cython Help

Singapore Python User Group, October 2018 Meetup

26 of 28

Getting Cython Help

Singapore Python User Group, October 2018 Meetup

27 of 28

28 of 28

Thank You

Questions?