7 ways to improve your Python performance

python1

 

Python is really a language which has swept the scene in recent years in terms of popularity, elegance, and functionality. Research shows that 8 out 10 computer science departments in the U.S. now teach their introductory courses with Python, surpassing Java. Top-ranked CS departments at MIT and UC Berkeley have switched their introductory courses to Python. And the top three MOOC providers (edX, Coursera, and Udacity) all offer introductory programming courses in Python. Not to mention, Python is known to have an easy syntax that makes learning much more tolerable, and it scales up easily – especially in cloud-based environments.

 

600x400 01 - 7 ways to improve your Python performance

 

As powerful as Python is, there’s always room for improvement. If you want to make your Python code even faster and more efficient, then stay tuned. Below we’ve listed 7 ways to improve the performance of your Python environment. Read on!

 

1. Use some of Python’s “speedup” applications

 

Python has developed a reputation as a solid, high-performance language. Lots has been done in recent years to get to this point. The PyPy project aims to speed up Python as a whole (and is doing a great job of it). And Numba is another tool that can offer amazing speedups by implementing high performance functions written directly in Python.

 

2. Using generators & sorting with keys

 

Generators are helpful in memory optimization because they allow you to create a function that returns one item at a time rather than all the items at once. A good example would be when you’re creating a large list of numbers and adding them together.

 

Also, when you’re sorting items in a list, be sure to use keys and the default sort() method whenever possible. In the following, notice that in each case the list is sorted according to the index you select as part of the key argument. This approach works just as well with strings as it does with numbers:

 

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)],

 

3. Using the latest releases of Python

 

Python is maintained through a community of developers who are committed to keeping the software current and robust. Each new release of the language is technically going to be faster and more optimized than before, so it’s a good idea to plan your move. Just be sure that your favorite supporting libraries are compatible with the newest versions.

 

python2

 

4. Avoid unwanted loops

 

The consensus is out that too much looping in any programming language is not a good thing, and puts unnecessary strain on your server. Some simple tweaks like storing the length of an array in a different variable instead of making it read the length at every iteration of the loop, can go a long way to optimizing your code and ensuring things run much more efficiently. Also, try refactoring your code to use intersections and unions. For example, instead of this:

 

for x in a:

 

    for y in b:

 

        if x == y:

 

            yield (x,y)

 

Try this instead:

 

return set(a) & set(b)

 

5. Try out multiple coding approaches

 

It’s typical to employ the same general coding techniques throughout your application. But why not try a little experimentation to see if one technique is better or more optimal than another. This will help keep you sharp and innovative in your approaches to coding, no matter what language you’re working in. By learning to “think outside the box” you’ll creatively apply new coding techniques to obtain faster results with your applications.

 

6. Keep Python code small and light

 

It is often the case in programming that simplest is fastest. In this day and age performance is critical and it’s especially important to keep your Python code as compact as possible to reduce latency and speed things up. One article provides some organizing questions that are worth asking in the development stage: “Do we really need this module?”, “Why are we using this framework? Is it worth the overhead?”, “Can we do this in a simpler way?”

 

7. Cloud-based application performance monitoring

 

Your Python-based applications and websites are only as good as the performance of your infrastructure, which is why application performance is too important to leave to chance. What this requires is the best-in-class monitoring capability for all your IT systems. That’s exactly what you’ll find in Monitis. As the trusted leader in this market, Monitis offers a 24/7 cloud-based monitoring platform for websites, servers, applications, and networks. Through an easy to follow and intuitive web-interface, Monitis tracks all of your business-critical applications to ensure everything is running optimally and that you’re alerted to issues long before your customers are.

 

You might also like

  • Santosh Kumar

    1 and 3 can easily be eliminated in my case. Where I write plugin for other commercial software where I have no control over whatever Python version they use, which usually are below the line, because they themselves have to test.
    At present, I have no choice working other than cPython implementation.

    • Anis Matar

      why don’t you use Cython in case of plugins the speed of C code it provides is unsurpassed. This way you get the best of both worlds.