5 Optimization Tips for Better Python Performance

Python got famous as a programming language in the recent decade. It is an excellent instrument assisting not only in backend development but also in data science and automation.

The simplicity of the language allows anybody to start learning it. Python has a straightforward syntax and that helps a lot in building software.

However, there are plenty of pitfalls and inefficient code can significantly slow down performance.

The good news is that it is possible to avoid such issues. Python has recommendations on how to make your code faster. What are those? Let’s explore.

Adopt Built-in Functions

Python developers try to perform best on the most common actions. They have implemented the functions to cover those actions. For example, we have min()max()sum() and many more handy functions.

They are built-in and available without any extra imports. And they demonstrate good performance.

So, every time, when you need to sum all elements in the array, consider using a built-in function sum() instead of implementing your own algorithm.

Utilize Set for Efficient Searching

Set is another data structure that is often forgotten. Software engineers actively use lists and dictionaries but rarely sets.

Yet when it comes to finding unique elements in the collection or quickly checking inclusion, a set demonstrates an extreme performance.

collection = [x for x in range(100_000)]
exists = 1000 in collection
>> Duration: 0.000015020

collection = set(x for x in range(100_000))
exists = 1000 in collection
>> Duration: 0.000001907

In the example from above, we see that checking an element’s presence in a set is ~7x faster than in a list.

Use Enumerate Instead of Range for Accessing Index

If you need to loop through the collection and have indexes, the best way to achieve that is by using enumerate.

This option is faster than looping through the range. And additionally, we don’t need to access the item by index as enumerate does that for us.

collection = range(0, 1_000_000)
for i in range(len(collection)):
print(collection[i])
>> Duration: 0.114128828

collection = range(0, 1_000_000)
for i, number in enumerate(collection):
print(number)
>> Duration: 0.062491894

We see that the case with enumerate is almost twice faster than the case with range in this example. It is because internally enumerate generates a special object for enumeration. It returns the item and the index effectively in one run.

Apply List Comprehension for Creating Lists

List comprehension is a widely used feature in Python. Looping through the list and converting elements can be done only in a single line.

But an extra point is that it has good performance. Let’s compare it with the regular for loop.

collection = range(0, 1_000_000)
newlist = []
for item in collection:
newlist.append(item)
>> Duration: 0.065476894

collection = range(0, 1_000_000)
newlist = [item for item in collection]
>> Duration: 0.024003267

This example demonstrates that list comprehension is three times faster.

Beginners usually struggle with the syntax of this feature especially when there are a few nested levels. But once they master it, they will apply it everywhere.

Use Join Instead of “+” to Concatenate Strings

When we think about string concatenation together, the first that comes to our minds is an operator +. Because it is intuitive. When the first string is 'hello,' and the second string is 'world'. To combine them we need to write 'hello,' + 'world'.

The issue with that way of string concatenation is performance. On every + operation, a new string will be created. Combining two strings in this way is not a big deal. But when it comes to the bigger number of strings, you will see performance degradation.

The efficient way to concatenate strings is to use join() function.

strings = ["Python", "is", "amazing", "when", "you", "follow", "pro", "tips", "and", "advice"]
string_concat = ""
for s in strings:
string_concat += s
>> Duration: 0.000010014

strings = ["Python", "is", "amazing", "when", "you", "follow", "pro", "tips", "and", "advice"]
string_concat = "".join(strings)
>> Duration: 0.000002861

The performance is already visible on a small number of strings. But the difference will become bigger with the increased number of strings to concatenate.

Conclusion

The list to improve the performance of your Python code can grow. Some of the advice can belong only to a specific scenario.

The tips we covered in this piece are considered best practices in Python. You probably will not notice any difference on small data sets. But when the amount of data you need to process is big enough, you will see significant performance wins.

Want to become a better software developer?

Do you want to grow professionally as a software engineer?
Are you curious about how to achieve the next level in your career?

My book Unlock the Code offers a comprehensive list of steps to boost
your professional life.