DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Speed up Your Python Code as a Good Programmer#PROTIPS⚑
vivek patel
vivek patel

Posted on • Updated on

Speed up Your Python Code as a Good Programmer#PROTIPS⚑


Python is very popular and easy to learn a language. but when it comes to Speed it is really slow. Here are Pro some tips on how you can speed up your python code as a good python programmer.


#1. Understand the basic data structures

dictionary and sets use hash tables so have O(1) lookup performance read The Hitchhiker’s Guide to know how it works.
Refer to O(1) vs O(n) an easy analogy or Time Complexity for the performance cheat sheet for all major data types.

it is often a good idea to use sets or dictionaries instead of lists
in cases where:

  • The collection will contain a large number of items
  • You will be repeatedly searching for items in the collection
  • You do not have duplicate items.

#2. Concatenation on strings

Strings are immutable in Python.

This fact often flashes and bites novice Python programmers on the rump, immutability offers some advantages and disadvantages.

  • in Advantage, strings can be used as keys in dictionaries and individual copies can be shared among multiple variable bindings. (Python automatically shares one- and two-character strings.)
  • as a disadvantage, you can't say something like, >"change all the 'a's to 'b's" in any given string. Instead, you have to create a new string with the desired properties. This continual copying can lead to significant inefficiencies in Python programs.

Avoid this:

s = ""
for substring in list:
    s += substring
Enter fullscreen mode Exit fullscreen mode

Use:

s = "".join(list)
Enter fullscreen mode Exit fullscreen mode

instead. The former is a very common and catastrophic mystery while creating large strings. Similarly, if you are generating bits of a string sequentially instead of:

s = ""
for x in list:
    s += some_function(x)
Enter fullscreen mode Exit fullscreen mode

Use list comprehension

slist = [some_function(elt) for elt in somelist]
s = "".join(slist)
Enter fullscreen mode Exit fullscreen mode

Avoid:

out = "<html>" + head + prologue + query + tail + "</html>"
Enter fullscreen mode Exit fullscreen mode

Use:

out = "<html>%s%s%s%s</html>" % (head, prologue, query, tail)
#or
out = f"<html>{head}{prologue}{query}{tail}</html>"
#or you can use 
out = "<html>{}{}{}{}</html>".format(head,prologue,query,tail)
Enter fullscreen mode Exit fullscreen mode

#3 map() and list comprehension Are Golden Keys!!!

Python supports a couple of looping constructs. The for statement is most commonly used. If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where the mapfunction is handy. map convert our for into C code. The only restriction is that the "loop body" of the map must be a function call. List comprehensions are often as fast or faster than equivalent use of the map.

newlist = []
for word in oldlist:
    newlist.append(word.upper()) #slow
Enter fullscreen mode Exit fullscreen mode

you can use the map function to convert for loop into compiled C code:

newlist = map(str.upper, oldlist) #fast
Enter fullscreen mode Exit fullscreen mode

You can also use list comprehension for good speed results!

newlist = [s.upper() for s in oldlist]
Enter fullscreen mode Exit fullscreen mode

#4 Working With Lazy libraries

import statements can be executed anywhere in code. It's a useful decision to place them inside functions to restrict their visibility and/or reduce initial startup time.

Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect your speed.

Avoid:

import tensorflow
Enter fullscreen mode Exit fullscreen mode

Use:

from tensorflow.keras.layers import Dense, Flatten, Conv2D
Enter fullscreen mode Exit fullscreen mode

A good way to do lazy imports is:

email = None

def parse_email():
    global email
    if email is None:
        import email
    ...
Enter fullscreen mode Exit fullscreen mode

#5.Use print when it necessary

Use the Print statement when you need it most because the print statement redirects output to our terminal.printing stuff to the console is a blocking operation, we would only include the prints if they are needed, once you remove the prints code will run faster


#6.use built-in modules more!

Python has many built-in functions implemented in C, which are very fast and well maintained. We should at least familiar with these function names and know where to find it (some commonly used computation-related functions are abs(), len(), max(), min(), set(), sum()).

Here Are Some useful links You are looking for

When to Use a List Comprehension in Python

Transforming Code into Beautiful, Idiomatic Python

Advantages and Disadvantages of Python Programming Language


As always, I welcome feedback, constructive criticism, and hearing about your projects. I can be reached on Linkedin, and also on my website.

Top comments (0)

Thank you.

Β 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.