How to optimize your Python apps

By: Iryen
  |  May 1, 2023
How to optimize your Python apps

Python optimization is the solution to speed performance issues. But, when do you optimize, and what parts of the code should be optimized? This article will help you answer these questions. 

Developers always want to efficiently write neat code. However, things are quite different when working with a Python-based data science project.

There will be situations where you need Python optimization. However, there are cases where optimization yields irrelevant results. Hence, it is important to examine the situation and determine if it is relevant to your case. Remember, Python optimization is always placing importance on what’s “behind the scenes.” 

Relevance of Python Code Optimization

The ideal approach of Python code optimization is getting into the habit of writing efficient codes. It is a skill for developers to spot lines or functions to improve right away.

When looking for ways to optimize Python codes, there could be a ton of options. When you are looking for ways to optimize your code, bear in mind the possibility of some trade-offs. For instance, which do you prefer? A robust piece of code or a simpler one?

The former may be complex but provides relevant results essential in dealing with data analytics.  The latter may be a simple code. Python is known for its “one-liner” coding structure, meaning it is easy to test and maintain.

Tips and Tricks

There are several tips and tricks to help you build faster Python applications. However, these tips may or may not apply to you. Hence, keep in mind to optimize only when necessary. 

The best way to perform Python optimization is to consider the time complexity of the different Python constructs. It is imperative to collect data and results to help that establish you’re optimizing it right.

Contrarily, it is difficult to impose optimization practices if you don’t experience it. You may implement some of the tips in your coding practice directly, but other tips need additional verification tools such as the use of code profiling methods that quickly lead to the road of optimization.

Profile Your Code

Achieve Python optimization by knowing where the bottlenecks lie. Don’t just jump into the bandwagon of optimization as it is useless if you don’t know the things that need it. Here are three ways to profile your code:

Use stop-watch profiling with <timeit>

A traditional way of profiling Python codes is using the timeit module. It is a method that is available through the Python library, timeit

How does it work? It measures the execution time taken for a small code. The library runs the code statement one million times. Then, it provides the minimum time taken. Hence, this method is useful in checking the performance of the code.

Syntax:

timeit.timeit(stmt, setup, timer, number)

Parameters

stmt: This points out the code that needs execution time measurement.It has a default value of “pass”.

setup: Refers to the setup details that need to be executed before stmt. It has a default value of “pass”.

timer: This is the timer value.

number: The stmt will execute as per the number is given but the default value is 1000000.

Sample Code:

# sample timeit code

import timeit

print(timeit.timeit(‘output=9*4’))

Running this code will result in 0.05538080000000001 which is the execution time.

cProfiler

The cProfiler is part of the Python package. It has excellent profiling features to isolate bottlenecks in the code. You can use it inside your code in many ways. For example, wrapping a function inside its run method to measure the performance. Also, run the entire Python script using the command line. This is possible while activating cProfile as an argument using Python’s “-m” option.

Stackify Profilers

Another way of performing Python optimization is the use of profilers. A profiler analyzes your code and finds bottlenecks. You can use cProfiler or opt for robust profilers such as Prefix and Retrace by Stackify. 

Prefix is a code profiler that understands the complexity of your code and traces bugs that you think didn’t exist. Retrace, on the other hand, is a full-cycle Application Performance Management(APM) that combines logging and monitoring to provide actionable insights. 

Want to know more? Start your two-week FREE Retrace trial today.

Generators and Keys

Using generators and keys for sorting is a great tool for memory optimization. Memory issues are common in Python applications. I’ve written an article about the top 5 Python memory profilers that you can check out as well to learn more about memory optimization. 

Going back to generators and keys, these items facilitate creating functions that can return one item at a time. Accordingly, it will not return all at once and is quite helpful when creating a huge list of numbers.

You may opt for keys and the default sort() method. Check on the example below wherein the code is sorting the list as per the selected index. It is a part of the key argument, which is also applicable for strings.

Sample Code:

import operator

test = [(23, 34, 45, 78), (16, 19, 12, 56), (33, 28, 26, 75)]

print(“Before sorting:”, test)

test.sort(key=operator.itemgetter(0))

print(“After sorting[1]: “, test)

test.sort(key=operator.itemgetter(1))

print(“After sorting[2]: “, test)

test.sort(key=operator.itemgetter(2))

print(“After sorting[3]: “, test)

Output: 

Before sorting: [(23, 34, 45, 78), (16, 19, 12, 56), (33, 28, 26, 75)]

After sorting[1]:  [(16, 19, 12, 56), (23, 34, 45, 78), (33, 28, 26, 75)]

After sorting[2]:  [(16, 19, 12, 56), (33, 28, 26, 75), (23, 34, 45, 78)]

After sorting[3]:  [(16, 19, 12, 56), (33, 28, 26, 75), (23, 34, 45, 78)]

Optimizing Loops

Mostly all programming languages require optimizing loops. Python has a way of transforming loops to perform faster. It is a simple method, but programmers often miss it, preventing dots within a loop.

But why do we need to optimize loops? It is important to note that the Python engine takes a lot of time interpreting the for loop construct. Therefore, it’s always a better choice to replace them with built-in constructs like Maps.

Additionally, since Python is a powerful language, several building blocks support looping. Out of a few types of loops, the “for” loop is prevalent, which can be costly. Hence, consider this when using loops every time you start coding.  

Use Set, avoid globals

Set operation in Python is the same as Mathematics. This includes union, intersection, and difference. Sets are way faster than iterating over the lists; you can use them in your code as you see it fit. 

Here are more detailed attributes of sets:

Syntax            Operation      Description

——            ———      ———–

set(14)|set(22)    Union       Set with all l4 and 22 items.

set(14)&set(22)    Intersection  Set with commmon l4 and 22 items.

set(14)-set(22)    Difference    Set with l4 items not in 22.

To manage sets, Python uses hash tables. So, whenever a programmer adds an element to a set, the Python interpreter determines its position. The position is the memory allocated for the set using the hash of the target element.

So, what is the relevance of the set? As Python executes automatic resizing of the hash table, the speed can be constant. This is consistent regardless of the size of the set. As a result, it makes the set operations faster.

On the other hand, you should avoid global variables. This is not exclusive to Python since almost all languages don’t recommend the use of globals. All programming experts disapprove of the excessive or unplanned use of global variables. The primary reason is that these globals may cause hidden or non-obvious side effects. 

These effects may lead to problems, in which instead of troubleshooting, they may have a Spaghetti code. Furthermore, Python is known to have slow performance in accessing external variables.

With that, Python is faster when retrieving a local variable. 

Sample Code:

# Sample code to illustrate using

# local variables to make code 

# run faster 

class Sample: 

    def func(x,y): 

        print (y+y) 

# Declaring variable that assigns class method object 

Obj = Sample() 

# Declaring local variable

mySample = Obj.func 

n = 10

for i in range(n): 

    mySample(i) # faster than Obj.func(i) 

Output: 

0

2

4

6

8

10

12

14

16

18

However, global variables are not banned. Python still permits its limited use when needed. Therefore, you can still declare an external variable using the global keyword. 

Use external libraries and built-in operators

Libraries and built-in operators are built for fast and efficient execution. Since Python is written in C, it has the same syntax. Some Python libraries have a C equivalent. So, being written in C makes these libraries and built-in operators perform faster. 

For instance, instead of using pickle, you should try using cPickle. Also, you can use Cython, which is an optimizing static compiler. Cython is a superset of Python, which brings support for C functions and types. It instructs the compiler to identify robust and efficient code.

In terms of packages, consider the PyPy package. It features a JIT (Just-in-time) compiler. This makes Python code run fast and even allows Python developers to tweak it to provide an extra processing boost.

Python programming language is based on high-level abstractions. Therefore, you should use the built-in operators as much as possible. These built-in operators are pre-compiled, making your code efficient and faster. It is way more efficient compared to lengthy iterations that include several interpreted steps that get very slow.

As an added tip, choose built-in features like the Python map() function that add significant speed improvements.

Strings

Is there a need to optimize strings? Yes, because string concatenation may slow your entire code. Best practices include eliminating string concatenation inside a loop. This is possible using Python’s join method. Another way is using the formatting feature to create a unified string.

Regular Expressions (RegEx) operations in Python is another method to optimize strings. It is a special sequence of characters that matches or finds other strings or sets of strings. A specialized syntax held in a pattern aids as it gets pushed back to C code. 

However, regex is not the ultimate string optimization tool. In some cases, basic string methods work better. Such strings include:

  • isalpha()
  • isdigit()
  • startswith()
  • endswith()

To help you test these different methods, you can use the timeit module. As stated earlier, it helps you determine which method performs best.

If Statement

Python works just like any other high-level programming language that allows lazy-if evaluation. The lazy evaluation joins ‘AND’ conditions. It will not test all conditions in case one of them returns false.

Developers can adjust their codes to utilize Python’s behavior. For instance, a problem that includes searching for a fixed pattern in a list is quite doable with this approach. First, reduce the scope by adding the following condition:

  1. Add an ‘AND’ condition that returns false if the target string’s size is less than the length of the pattern.
  2. Then, test a fast condition(e.g., you may use something like this, “string should start with an @” or “string should end with a dot”).

Python Application Optimization 

Python is a powerful programming language with a fair amount of advanced approaches to managing your code. You may start from parallelism or concurrency to different tricks. And speaking of tricks, how about designing smaller objects to fit in the cache layer of the heap memory instead of the main one?

This sounds tricky but doable.

Indeed, there are a lot of optimization options. It would vary and depend on the type of application you’re doing. For some applications, one can use libraries that are designed to optimize the needed tasks. However, this guide selected the common one that most Python beginners implement. 

So, how did you optimize your Python code, and what tricks and styles are you using?

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]