# Python Tutorial : Use Map Function to speedup your python code

There are many situations when similar operations have to performed on a complete list of data’s. A beginner will write for/while loop to perform this.

But wait, there is a better and faster way to do this in python. This is where map() function . It is faster and can be done in just one line.

Consider following example where, you need to carry find a square of all the numbers in a list. A normal way to this will be

number = [1,2,3,4]
res = [] #Initialize Result
def square(x):
return x*x
for num in numbers:
res.append(square(num))
res #This will give output as[1,2,3,4]

However, with map() function you can completely eliminate for loop.

## Syntax of map()

map( fun_name, iterable)

<strong>map():</strong> function executes a specified function with name fun_name for each item in an iterable. The item is pass to the function as a parameter.

fun_name: It is a function to which map() passes each element of given an iterable.

iterable: It is an array of data (list, tuple, dictionary etc) whose each data is passed to map(). Number of iterable is equal to number of argument fun_name takes

map() function could be used in the above python example as follows

# Example of map function
number = [1,2,3,4]
def square(x): # This function requires one argument
return x*x
res=map(square,number) 3 This function applies
res # This will print map object
list(res) # This will give output as [1, 4, 9, 16]

### Speed test of map function

Map function has a lot of potential to speedup python function. The following program will show the capability of speedup of python function by using map function.

# sum Function
import time

def sum(x,y):
return x+y

n=int(5.0e5)
x = range(n)
y = range(n)

#--------------Map function with 2 arguments----------
t1=time.time()  # Gives wall time
res=map(sum,x,y)  # applies sum to each and every element of x and y array
res1=list(res)

#-----------------Usual way--------------
t2=time.time()  # Gives wall time
res=[0.0]*len(x)  # Initialize
index=0
for i in range(len(x)):
res[i] = sum(x[i],y[i])
t3 = time.time()

print(" Time using map function = ", t2-t1)
print(" Time using for loop  = ", t3-t2)

# Output
>>Time using map function =  0.8940510749816895
>>Time using for loop  =  3.0341737270355225

Under the above case it can be seen that map function can speed up python function by up to 3.5 times. This performance can increase for more computationally extensive function.

Map function may not always speedup python function. For a small number of arguments map function could be slower than pure python function without map. For an example with 1000 arguments in above example, map function is around 12 times slower.

# Speed test of map function for 1000 iterator
# replace n=int(5.0e5) in above code with, every other thing remains constant
n = int(1e3)
>>Time using map function =  0.08400487899780273
>>Time using for loop  =  0.008000612258911133

## Conclusion

Map function provides is great way to reduce loops in python code. It can also give performance gains while applying function on large array of elements. However for smaller arrays of elements, map function of python could be slower as it has its own overheads. Its always better to profile code and check for sweet spot after which map() function is efficient.