Effective Programmer

Real advice to move your career forward as a software engineer from people who have been around.

Follow publication

Member-only story

Featured

High Performance Python

--

If you’ve read my previous posts, you know I’m a huge Python fan and recommend everyone learn it. However, Python is a high level interpreted language and can sometimes face performance challenges compared to lower-level languages. Today, I want to explore some techniques to significantly enhance your code’s performance.

I’ll do my best to explain why these approaches work and their quantifiable speed improvements but remember that each use case is different and your mileage may very, let me know in the comments below if any of these did or did not work for you. Lets get to it.

1. Vectorization with NumPy

Normally, Python loops process data elements sequentially, with each iteration incurring a fixed amount of interpretation overhead. NumPy, in contrast, implements operations at the C level, processing entire arrays of data in optimized, compiled code.

When we execute operations on NumPy arrays, we’re effectively transferring computational work from Python’s interpreter to pre-compiled, highly optimized C functions. This approach is called vectorization.

Consider these two approaches for doubling each number in a large sequence:

# Traditional Python loop approach
def double_with_loop(n):
result = []
for i in range(n):
result.append(i * 2)…

--

--

Published in Effective Programmer

Real advice to move your career forward as a software engineer from people who have been around.

Written by Naveed Khan

Technology leader, entrepreneur and angel investor. Head of Engineering at Blitz.gg. I write about programming, software engineering and career development.

No responses yet

Write a response