In this article, you will learn how to make python codes run faster by applying some basic and easy optimization techniques.
The python codes will become shorter and easy to understand as compared to the codes written before. Doing this will not only produces the desired output faster but also reduces the effort to write the lengthy codes.
Here are some tips to optimize your python codes that will result in the faster execution of the program. You can Also Join Our Python Course to learn how to make code faster.
[1] Multiple Assignment
Instead of assigning values to two or more variables one by one, you can use multiple assignment technique for the same task. Let’s give you a simple example of swapping two variables in a single line without using a temporary variable.
Traditional Slower Way
a=4
b=2
temp=a
a=b
b=temp
print “Swapped values :”, x,y
Modern Faster Way
a,b=3,8
a,b=b,y
print “Swapped values :”, a,b
[2] Map() function
In python, there are several built-in library functions that enables a programmer to make python codes run faster, save time and speedup the execution process.
Map() is one of those built-in library function used for speeding up the execution process. It applies a given function to each item of an iterable(list, tuples, etc.) and returns the result in the form of a list.
The syntax of map() function is shown below:
map(function, iterable)
- Function-each iterable is passed to this function for mapping.
- Iterable-it may be a list, tuple, etc. to be mapped.
Using map() function, a programmer doesn’t need to apply a loop, thus making the whole program run faster.
We have shown below two different examples with different codes for the same output. In the first one, we have applied ‘for loop’ and in the second one we have used map() function.
Let’s see which one of these executes faster.
Traditional Slower Way
import time
begin = time.clock()
b = ‘W3TS’
L = []
for i in b:
L.append(i.lower())
print L
passed = time.clock()
p1 = passed – begin
print “Total time spent in the execution process using for loop : “, p1
Output:
[‘w’, ‘3’, ‘t’, ‘s’]
Total time spent in the execution process using for loop : 0.00709003566832
Modern Faster Way
import time
b = ‘W3TS’
begin = time.clock()
L = map(str.lower, b)
print L
passed = time.clock()
p2 = passed – begin
print “Total time spent in the execution process using map() function: “, p2
Output:
[‘w’, ‘3’, ‘t’, ‘s’]
Total time spent in the execution process using map() function: 0.00381961984303
Clearly, if you compare the time spent in the execution process in both conditions, the map() function produces the result in a very less time.
So, it will be beneficial for you to use built-in library functions to make python codes run faster.
[3] List Comprehension
Use list comprehension to iterate a range instead of using for loop. It is the faster way to get the output and requires only a single line to finish and execute the codes.
Let’s have a look on how for loop and list comprehension works.
For loop representation :
for(values to iterate):
if (conditional filtering):
output expression()
List Comprehension Representation :
[output expression() for(values to iterate) if(conditional filtering)]
Clearly, using list comprehension, you can finish a multi line code in a single line. This will also make python codes run faster.
Now, let’s implement these techniques by a working example.
Traditional Slower Way: Using for loop
even=[]
for i in range(10):
if i%2==0:
even.append(i)
print even
Output:
[0, 2, 4, 6, 8]
Modern Faster Way: Using List Comprehension
even=[i for i in range(10) if i%2==0]
print even
Output:
[0, 2, 4, 6, 8]
[4] Dictionary Comprehension
You can also use dictionary comprehension to make python codes run faster. We have shown below an example of pair of a number and its square using for loop and dictionary comprehension.
Traditional Slower Way: Using for loop
even={}
for i in range(1,8):
even[i]=i*i
print even
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
Modern Faster Way: Using Dictionary Comprehension
even={i:i*i for i in range(1,8)}
print even
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}
[5] range() and xrange() function
The xrange() function gives faster results as compared to the range() function.
Using range() function
a=[x for x in range(2,16,2)]
print a
Output:
[2, 4, 6, 8, 10, 12, 14]
Using xrange() function
a=[x for x in xrange(2,16,2)]
print a
Output:
[2, 4, 6, 8, 10, 12, 14]
There are many optimization techniques to make python codes run faster. We have shown you five of them. Hope this information will help you to get your things done regarding python programming language.