From the performance point of view which method is faster: dictionary or vars()[]....?
I test the speed:
I test the speed:
import time my = {} start_time = time.time() my["clock"] = time print(my["clock"].localtime()) print("--- %s seconds ---" % (time.time() - start_time)) start_time = time.time() vars()["clock"] = time print(vars()["clock"].localtime()) print("--- %s seconds ---" % (time.time() - start_time))
Output:time.struct_time(tm_year=2020, tm_mon=6, tm_mday=20, tm_hour=17, tm_min=40, tm_sec=0, tm_wday=5, tm_yday=172, tm_isdst=1)
--- 0.02257513999938965 seconds ---
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=20, tm_hour=17, tm_min=40, tm_sec=0, tm_wday=5, tm_yday=172, tm_isdst=1)
--- 0.0029897689819335938 seconds ---
So, the dictionary method is 10x slower than vars()[] method.