Faster Python Code
Faster Python Code
In [6]: # Slow:
start = time.time()
new_list = []
new_list.append(word.capitalize())
4.935264587402344e-05 seconds
In [7]: #fast
start = time.time()
0.00029468536376953125 seconds
start = time.time()
new_list = []
new_list += word
5.7220458984375e-05 seconds
In [10]: #fast
start = time.time()
new_list = "".join(word_list)
0.0001430511474609375 seconds
list()
dict()
{}
Out[11]:
In [12]: # fast
()
{}
{}
Out[12]:
print("faster_list:",faster_list, "sec")
print("faster_dict:",faster_dict, "sec")
f-Strings
In [22]: #slow
start = time.time()
me = "Python"
4.506111145019531e-05 seconds
In [23]: #fast
start = time.time()
me = "Python"
0.00016546249389648438 seconds
List Comprehensions
In [24]: #slow
start = time.time()
new_list = []
existing_list = range(1000000)
for i in existing_list:
if i % 2 == 1:
new_list.append(i)
0.06872344017028809 seconds
In [25]: #fast
start = time.time()
existing_list = range(1000000)
0.04211759567260742 seconds
In [ ]: