Eklablog All blogs Top blogs Tech & Science
Edit post Follow this blog Administration + Create my blog
MENU

Advertising

Python make it much faster !

#python programming today we will study map function to make functions run faster 

import time # we will record every function and make comparison of speed let go

time_at_this_moment_in_seconds=time.time()

 

def function1(listinput): #this is first example

 result=[]

 for element in listinput:                # or we can write  for first_argument,second_argument in listinput: and deleting this line "first_argument,second_argument=element "

   first_argument,second_argument=element   

   result.append(first_argument+second_argument)

 return result

short_list_as_input_for_exemple=[(1,2),(3,4),(5,6)]

short_list_made_longer=[(1,2),(3,4),(5,6)]*10

print("short_list_made_longer:",short_list_made_longer)

result_of_function1=function1([(1,2),(3,4),(5,6)]*1000)# let now make the input more longer and see how much second and comparison percents will change

print("result_of_function1:",result_of_function1)

time_once_first_function_finish=time.time()

time_that_took_the_function1_to_finish=time_once_first_function_finish-time_at_this_moment_in_seconds

print("time_that_took_the_function1_to_finish:",time_that_took_the_function1_to_finish)

def function2(listinput): #let go for second example 

 def minifunction(element):

   first_argument,second_argument=element  

   return  first_argument+second_argument

 

 return list(map(minifunction,listinput))

time_at_this_newmoment_in_seconds=time.time()

result_of_function2=function2([(1,2),(3,4),(5,6)]*1000) # we give same input to both functions

print("result_of_function2:",result_of_function2)

print(result_of_function2==result_of_function1,1==1)

time_once_first_function2_finish=time.time()

time_that_took_the_function2_to_finish=time_once_first_function2_finish-time_at_this_newmoment_in_seconds

print("time_that_took_the_function1_to_finish:",time_that_took_the_function2_to_finish)

print("calculating percent of comparison(how much function2 is faster then function 1):")

print((time_that_took_the_function2_to_finish/time_that_took_the_function1_to_finish)*100,"%")

print("calculating difference time between time took two functions(how much seconds function2 is faster than function 1):")

print((time_that_took_the_function1_to_finish-time_that_took_the_function2_to_finish),"seconds")

#Conclusion

#more we have longer list more we win time if we use map builtin function

#thanks for watching add like only if you like and follow for news videos and vote or suggest for new videos subjects .see you soon

 

 

Advertising
Back to home page
Share this post
Repost0
To be informed of the latest articles, subscribe:
Comment on this post