Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dict_order_vs_loop_order.py
- from time import perf_counter
- old_list = ('a', 'b', 'a', 'b', 'c', 'a', '3', 'd', 'b', '2', 'e', 'c', 'a', '1', 'e', 'c', '3')
- for i in '.'*20:
- print(tuple(dict.fromkeys(old_list)))
- old_list = [old_list for _ in range(2_000_000)]
- for i in '.'*60:
- start = perf_counter()
- new_list = tuple(dict.fromkeys(old_list))
- dict_time = perf_counter()-start
- start = perf_counter()
- new_list = []
- for item in old_list:
- if item not in new_list:
- new_list.append(item)
- loop_time = perf_counter()-start
- print(f"")
- print(f"dict_time = {dict_time:0f}")
- print(f"loop_time = {loop_time:0f}")
- a, b = 'dict', 'loop'
- t = dict_time/loop_time
- if dict_time > loop_time:
- a, b = b, a
- t = loop_time/dict_time
- print(f"{a} time is {t:0f} times faster then {b}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement