I wanted to use GCP's Cloud Functions to convert the input to a pair of words each time, such as "fruit" for "apple" and "vegetable" for "strawberry". I was wondering which is faster, ** converting with if statement ** or ** preparing a dictionary type correspondence table and converting **, so I tried it. Note) In my case, it is often a cold start, so I calculate the required time including dictionary type generation.
** If dictionary reading occurs every time (such as AWS Lambda or GCP Cloud Functions cold start) ** The if statement is faster.
If the key is 5 characters
If the key is 10 characters (Because I used Colab, the processing speed of each time may not be stable)
** It was an overwhelming victory of the if statement. ** **
If it is not a cold start, I feel that the dictionary type is faster, so I will try it if I feel like it (If document is annoying ...)
Python 3 Google Colaboratory
-Execute the code to compare ** in the same cell 100 times by changing the number of keys, and compare by the average time required. ・ There are 6 patterns of 5, 10, 25, 50, 100, and 200 keys.
Preparation
import time
import random, string
from statistics import mean
#Random string creation (borrowed from the reference "Generate random strings using Python")
def randomname(n):
return ''.join(random.choices(string.ascii_letters + string.digits, k=n))
range_ = 5
smp_dict = {}
key_list = []
for i in range(range_):
key = randomname(10)
smp_dict[key] = randomname(20)
key_list.append(key)
#if statement creation
for i in range(range_):
if i == 0:
print(" if key == '{}':".format(key_list[i]))
else :
print(" elif key == '{}':".format(key_list[i]))
print(" content = '{}'".format(smp_dict[key_list[i]]))
Create verification code by copying the if statement output above
rnd_key_list = smp_dict.keys()
time_list = []
for i in range(100):
start = time.time()
for key in rnd_key_list:
if key == 's9MT4kkqyg':
content = 'JpNeK8tDV1FyQfXdzTS0'
elif key == 'PKFEikSHFM':
content = 'RxhZqdfsCqLTuxBzIrdV'
elif key == '04Ex4druSy':
content = 'GmT0Jx78xPuMrOyVPuik'
elif key == 'JUJ85l8ayb':
content = 'xVtzS0HnHHEOHML87z85'
elif key == 'HnqyHAIcLm':
content = 'NRNoem9JpGUcFxvccaxD'
time_list.append(elapsed_time)
if_mean = mean(time_list)
print ("if Mean time:{}".format(if_mean) + "[sec]")
time_list = []
for i in range(100):
start = time.time()
Smp_dict = {'s9MT4kkqyg': 'JpNeK8tDV1FyQfXdzTS0', 'PKFEikSHFM': 'RxhZqdfsCqLTuxBzIrdV', '04Ex4druSy': 'GmT0Jx78xPuMrOyVPuik', 'JUJ85l8ayb': 'xVtzS0HnHHEOHML87z85', 'HnqyHAIcLm': 'NRNoem9JpGUcFxvccaxD'}
for key in rnd_key_list:
content = Smp_dict[key]
elapsed_time = time.time() - start
time_list.append(elapsed_time)
dict_mean = mean(time_list)
print ("dict Mean time:{}".format(dict_mean) + "[sec]")
smp_dict = {}
key_list = []
for i in range(100):
key = randomname(10)
smp_dict[key] = randomname(20)
key_list.append(key)
-> Create a dictionary type & key list with 10 random characters as keys and 20 random characters as values
rnd_key_list = smp_dict.keys()
-> ~~ The order is not preserved when creating a dictionary type ~~, so create a new key list to make the conditions the same. I received a comment from @shiracamus that I will keep it recently!
start = time.time()
Smp_dict = {'BGM85yImoJ': 'i5GozNMNXGdfrlVgmVA0', ...}
-> Read dictionary type after starting measurement
[Python] Measure and display the time required for processing Generate random strings using Python
Recommended Posts