Speed up Your Python Code as a Good Programmer#PROTIPS⚡
Tags: python, showdev, datascience, machinelearning
Published: 2020-09-20 06:51:01

***

*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](https://docs.python-guide.org/writing/style/) to know how it works.
Refer to [O(1) vs O(n) an easy analogy ](https://www.instagram.com/p/CEf-0fOpCE9/) or [Time Complexity](https://en.wikipedia.org/wiki/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:_**
```python
s = ""
for substring in list:
    s += substring
```
**_Use:_**
```python 
s = "".join(list)
``` 
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:
```python
s = ""
for x in list:
    s += some_function(x)
```
**_Use list comprehension_**
```python
slist = [some_function(elt) for elt in somelist]
s = "".join(slist)
```

**_Avoid:_**
```python
out = "<html>" + head + prologue + query + tail + "</html>"
```
***_Use:_***
```python
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)
```

***
**#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 [map](https://www.geeksforgeeks.org/python-map-function/)function 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.
```python 
newlist = []
for word in oldlist:
    newlist.append(word.upper()) #slow
```
you can use the map function to convert for loop into compiled C code:
```python
newlist = map(str.upper, oldlist) #fast
```
You can also use list comprehension for good speed results!
```python
newlist = [s.upper() for s in oldlist]
```
***
**#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:_**
```python
import tensorflow
```
**_Use:_**
```python
from tensorflow.keras.layers import Dense, Flatten, Conv2D
```
A good way to do lazy imports is:
```python 
email = None

def parse_email():
    global email
    if email is None:
        import email
    ...
```

***
**#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](https://realpython.com/list-comprehension-python/)

[Transforming Code into Beautiful, Idiomatic Python](https://www.youtube.com/watch?v=OSGv2VnC0go)

[Advantages and Disadvantages of Python Programming Language](https://medium.com/@mindfiresolutions.usa/advantages-and-disadvantages-of-python-programming-language-fd0b394f2121)

---
*As always, I welcome feedback, constructive criticism, and hearing about your projects. I can be reached on [Linkedin](https://www.linkedin.com/in/vivek-patel-0553731a5), and also on [my website](vivolscute.github.io).*