Fast Python
Master the basics to write faster code
Presented by Chris Conlan for DC Python Meetup
Disclaimer
• This is a short presentation.
Story Time
Ways to speed up Python code
1. Optimize
2. Explore
3. Compile
4. Distribute
Ways to speed up Python code
1. Optimize – Eliminate unnecessary operations
2. Explore – Try something different
3. Compile – Use compiled code
4. Distribute – Use multiple threads
Ways to speed up Python code
1. Optimize – Eliminate unnecessary operations
• Store and reuse computed values
• Reduce time complexity
• Reduce memory complexity
2. Explore – Try something different
• Change something random to get the same result, see if it is faster
• Try another library or built-in function, see if it is faster
3. Compile – Use compiled code
• Used C-level algorithms from compiled libraries
• JIT compilation
• Vectorization
4. Distribute – Use multiple threads
• Parallelization/multi-core processing
• Async processing
• Run it on multiple computers
Optimize
Compile
Explore
Distribute
My code is slow!
What people tend to do …
Optimize
Compile
Explore
Distribute
My code is slow!
What people should do …
Baseline – Compute a cumulative sum 𝑂 𝑛2
Optimize – Compute a cumulative sum 𝑂 𝑛
Compile – Compute a cumulative sum Compiled +
Vectorized
Explore – Compute a cumulative sum Different
library
Distribute – Compute a cumulative sum
Let’s talk about that.
• Are there multiple data sources?
• Are you running the function multiple times?
• Where is the input data stored?
• Where is the output data stored?
• Can you do this work while something else is happening?
Distribution, parallelization, and async are a last resort.
Don’t do this
Instead, do this
Compilation
• Redundant operations are found in the code via dataflow analysis
• Example code in C programming language:
int A = 5;
int B = 6;
for (int i = 0; i < 10; i++) {
sum += A + B + i;
sum -= A + B – i;
}
Compiler performs dataflow analysis and
uses registers for intermediate values
Data is given explicit “integer” type;
statically allocated as number with no
object overhead
Vectorization
https://www.cs.utexas.edu/~pingali/CS380C/2016/lectures/david-vectorization.pdf
Results
Function Values Milliseconds to
compute
Values per
millisecond
slow_cusum 10,000,000 Weeks to months Around 1/10,000
python_fast_cusum 10,000,000 10,000 9,600
pandas_fast_cusum 10,000,000 780 130,000
np_fast_cusum 10,000,000 350 280,000
Bonus: JIT compile with numba
Things you could probably be doing faster
• Arithmetic
• Sorting
• Building data structures
• Vector computations
• Matrix computations
• File I/O
• Filesystem computations
• Counting
• Aggregation operations
• Database operations
See this process for 20
essential Python algorithms.
Amazon: https://amzn.to/3fmQja2
GitHub: https://github.com/chrisconlan/fast-python
Want a free copy? I have 10.
Send your name and address
to chris@conlan.io
Special thanks
• Janice McMahon for being one half of the original “Algorithms 101 for
Data Scientists” lecture, and for writing a good number of these
slides.
• The Bethesda Data Science Meetup for inspiring this talk and the new
book, Fast Python.
• Ying Wang and Will Angel for pulling all us together for this
presentation.

Fast Python: Master the Basics to Write Faster Code

  • 1.
    Fast Python Master thebasics to write faster code Presented by Chris Conlan for DC Python Meetup
  • 2.
    Disclaimer • This isa short presentation.
  • 3.
  • 4.
    Ways to speedup Python code 1. Optimize 2. Explore 3. Compile 4. Distribute
  • 5.
    Ways to speedup Python code 1. Optimize – Eliminate unnecessary operations 2. Explore – Try something different 3. Compile – Use compiled code 4. Distribute – Use multiple threads
  • 6.
    Ways to speedup Python code 1. Optimize – Eliminate unnecessary operations • Store and reuse computed values • Reduce time complexity • Reduce memory complexity 2. Explore – Try something different • Change something random to get the same result, see if it is faster • Try another library or built-in function, see if it is faster 3. Compile – Use compiled code • Used C-level algorithms from compiled libraries • JIT compilation • Vectorization 4. Distribute – Use multiple threads • Parallelization/multi-core processing • Async processing • Run it on multiple computers
  • 7.
    Optimize Compile Explore Distribute My code isslow! What people tend to do …
  • 8.
    Optimize Compile Explore Distribute My code isslow! What people should do …
  • 9.
    Baseline – Computea cumulative sum 𝑂 𝑛2
  • 10.
    Optimize – Computea cumulative sum 𝑂 𝑛
  • 11.
    Compile – Computea cumulative sum Compiled + Vectorized
  • 12.
    Explore – Computea cumulative sum Different library
  • 13.
    Distribute – Computea cumulative sum Let’s talk about that. • Are there multiple data sources? • Are you running the function multiple times? • Where is the input data stored? • Where is the output data stored? • Can you do this work while something else is happening? Distribution, parallelization, and async are a last resort.
  • 14.
  • 15.
  • 16.
    Compilation • Redundant operationsare found in the code via dataflow analysis • Example code in C programming language: int A = 5; int B = 6; for (int i = 0; i < 10; i++) { sum += A + B + i; sum -= A + B – i; } Compiler performs dataflow analysis and uses registers for intermediate values Data is given explicit “integer” type; statically allocated as number with no object overhead
  • 17.
  • 18.
    Results Function Values Millisecondsto compute Values per millisecond slow_cusum 10,000,000 Weeks to months Around 1/10,000 python_fast_cusum 10,000,000 10,000 9,600 pandas_fast_cusum 10,000,000 780 130,000 np_fast_cusum 10,000,000 350 280,000
  • 21.
  • 22.
    Things you couldprobably be doing faster • Arithmetic • Sorting • Building data structures • Vector computations • Matrix computations • File I/O • Filesystem computations • Counting • Aggregation operations • Database operations
  • 23.
    See this processfor 20 essential Python algorithms. Amazon: https://amzn.to/3fmQja2 GitHub: https://github.com/chrisconlan/fast-python
  • 24.
    Want a freecopy? I have 10. Send your name and address to chris@conlan.io
  • 25.
    Special thanks • JaniceMcMahon for being one half of the original “Algorithms 101 for Data Scientists” lecture, and for writing a good number of these slides. • The Bethesda Data Science Meetup for inspiring this talk and the new book, Fast Python. • Ying Wang and Will Angel for pulling all us together for this presentation.