Six ways to make Python programs more efficient

  • 2020-04-02 14:47:51
  • OfStack

Python is a cool language because you can do a lot of things in a very short time with very little code. Not only that, it also easily supports multi-tasking, such as multi-process. Python critics sometimes say that Python is slow. This article tries to introduce six tips to speed up your Python application.
1. Make critical code dependent on external packages

While Python makes many programming tasks easy, it may not always provide the best performance for urgent tasks. You can use external packages written in C, C++, or machine language for urgent tasks to improve the performance of your application. These packages are not cross-platform, which means you need to find the right package based on the platform you are using. In short, the solution gives up some application portability in exchange for program performance that can only be achieved by programming directly on a particular host. Here are some packages you should consider adding to your "performance Arsenal" :

    (link: http://cython.org/)       (link: http://pyinline.sourceforge.net/)       (link: http://pypy.org/)     (link: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/)

These packages improve performance in different ways. For example, Pyrex extends what Python can do, such as using C's data types to make memory tasks more efficient or straightforward. PyInIne lets you use C code directly in Python applications. The inline code in the program compiles separately, but it also keeps all the code in one place while taking advantage of the efficiency that C provides.
2. Use key when sorting

There's a lot of old Python sorting code that takes your time to create a custom sort, but it does speed up the sorting process at run time. The best way to sort elements is to use key and the default sort() sort method whenever possible. For example, consider the following code:


import operator
somelist = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]
somelist.sort(key=operator.itemgetter(0))
somelist
#Output = [(1, 5, 8), (6, 2, 4), (9, 7, 5)]
somelist.sort(key=operator.itemgetter(1))
somelist
#Output = [(6, 2, 4), (1, 5, 8), (9, 7, 5)]
somelist.sort(key=operator.itemgetter(2))
somelist
#Output = [(6, 2, 4), (9, 7, 5), (1, 5, 8)],

In each instance, the array is sorted according to the index you selected as part of the key argument. Similar to sorting by Numbers, this method also applies to sorting by strings.
3. Optimize the loop

Each programming language emphasizes the need to optimize the loop. When using Python, you can rely on a number of tricks to make loops run faster. However, one approach that developers often miss is to avoid using dot operations in a loop. For example, consider the following code:


lowerlist = ['this', 'is', 'lowercase']
upper = str.upper
upperlist = []
append = upperlist.append
for word in lowerlist:
  append(upper(word))
  print(upperlist)
  #Output = ['THIS', 'IS', 'LOWERCASE']

Every time you call the method STR. Upper, Python evaluates that method. However, if you replace the evaluated value with a variable, the value becomes known, and Python can perform the task faster. The key to optimizing the loop is to reduce the amount of Python execution within the loop, because Python's native interpreter really slows down execution in that case.

Note: there are many ways to optimize a loop, this is just one of them. For example, many programmers would say that list derivation is the best way to speed up execution in a loop. The key here is that optimizing the loop is one of the better ways for the program to achieve higher execution speed.
Use a newer version of Python

Search the web for Python information and you'll find countless people asking about the question of moving from one version of Python to another. In general, each version of Python contains optimizations that make it run faster than the previous version. The limiting factor for version migration is whether your favorite libraries have been migrated to newer versions of Python. Rather than asking whether a version migration should take place, the key is to determine when a new version has enough support to make the migration viable.

You need to verify that your code is still running. You'll need to use the new library you've acquired under the new version of Python, and then check to see if your application needs major changes. Only after you have made the necessary corrections will you notice the difference between the versions. However, if you happen to make sure that your application works with the new version without any changes, you may miss out on the new features that come with those version upgrades. Once you've migrated, you should write a description for your new version of the application, check for problems, and prioritize updating those with the new version's features. This way users will see a greater performance improvement earlier in the upgrade process.
5. Try a variety of coding methods

If you code the same way every time you create an application, it will almost certainly result in your application running more slowly than it can. As part of the analysis process, you can try some experiments. For example, to manage some elements in a dictionary, you can either safely determine whether the element already exists and is updated, or you can simply add the element and handle the non-existence of the element as an exception. Consider the first coding example:


n = 16
myDict = {}
for i in range(0, n):
  char = 'abcd'[i%4]
  if char not in myDict:
    myDict[char] = 0
    myDict[char] += 1
    print(myDict)

This code typically runs faster when myDict starts empty. However, another approach works better when mydict is usually filled with data (or at least most of it).


n = 16
myDict = {}
for i in range(0, n):
  char = 'abcd'[i%4]
  try:
    myDict[char] += 1
  except KeyError:
    myDict[char] = 1
  print(myDict)

Both cases have the same output: {' d': 4, 'c': 4,' b': 4, 'a': 4}. The only difference is how the output is obtained. Think outside the box and create new coding techniques that can help you leverage your application for faster results.
6. Cross-compile the application

Developers sometimes forget that computers don't actually understand any of the languages used to create modern applications, and that all computers understand is machine code. In order to be able to run applications on a computer, you use an application that converts human-readable code into something that the computer can understand. Sometimes it makes sense to write an application in one language, like Python, and run it in another language, like C++, from a performance standpoint. It depends on what you want the application to do and the resources the host system can provide.

An interesting cross-compiler, Nuitka, converts your Python code into C++ code. The result is that you can execute the application in native mode instead of relying on the interpreter. Depending on the platform and task, you can see a significant performance improvement.

(note: Nuitka is still in the testing phase, so be careful when using it for production applications. In fact, it is better to experiment with it now. There is also some discussion about whether cross-compilation is the best way to get better performance. Developers have been using cross-compilation for years to achieve specific goals, such as better application speed. Remember, every solution has its trade-offs, and you should think about the trade-offs before applying a solution to a production environment.

When using a cross-compiler, make sure it supports the version of Python you are using. Nuitka supports Python2.6, 2.7, 3.2, and 3.3. To make this work, you need a Python interpreter and a C++ compiler. Support for a variety of C++ compilers, including Microsoft Visual Studio, MinGW, and Clang/LLVM.

Cross-compilation can also have some serious negative effects. For example, when working with Nuitka, you'll find that even a small program can consume a lot of hard disk space, because Nuitka USES a lot of dynamic link libraries (DLLs) for Python functions. So when you're dealing with a system with limited resources, it may not work very well.
conclusion

Any of these six tricks can help you create faster Python programs. But no technique is a panacea. It doesn't work every time. Some techniques work better with a particular version of Python than others -- even the system platform can affect their effects. You need to configure your application, determine where to slow it down, and then try out some techniques that seem best to solve these problems.


Related articles: