Created
April 27, 2021 20:05
-
-
Save jwhitlock/3ddaad9a06d387e54e2e1d530b248575 to your computer and use it in GitHub Desktop.
Python3 sets vs lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Timing set_in_loop... 1.063s | |
Timing list_in_loop... 1.835s | |
Timing set_out_loop... 1.093s | |
Timing list_out_loop... 1.998s | |
function time factor | |
set_in_loop 1.063s (1.0x) | |
set_out_loop 1.093s (1.0x) | |
list_in_loop 1.835s (1.7x) | |
list_out_loop 1.998s (1.9x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Timing set_in_loop... 1.041s | |
Timing list_in_loop... 1.848s | |
Timing set_out_loop... 1.182s | |
Timing list_out_loop... 2.019s | |
function time factor | |
set_in_loop 1.041s (1.0x) | |
set_out_loop 1.182s (1.1x) | |
list_in_loop 1.848s (1.8x) | |
list_out_loop 2.019s (1.9x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
from timeit import timeit | |
STRINGS = [ | |
"amo_display_name", | |
"amo_homepage", | |
"amo_id", | |
"amo_last_login", | |
"amo_location", | |
"amo_user", | |
"country", | |
"created_date", | |
"email", | |
"email_id", | |
"first_name", | |
"format", | |
"fpn_country", | |
"fpn_platform", | |
"fxa_create_date", | |
"fxa_deleted", | |
"fxa_id", | |
"fxa_lang", | |
"fxa_primary_email", | |
"fxa_service", | |
"id", | |
"lang", | |
"last_modified_date", | |
"last_name", | |
"mofo_relevant", | |
"newsletters", | |
"optin", | |
"optout", | |
"reason", | |
"token", | |
] | |
def set_in_loop(): | |
for x in STRINGS: | |
if x in {"lang", "format"}: | |
pass | |
def list_in_loop(): | |
for x in STRINGS: | |
if x in ["lang", "format"]: | |
pass | |
AS_SET = {"lang", "format"} | |
AS_LIST = ["lang", "format"] | |
def set_out_loop(): | |
for x in STRINGS: | |
if x in AS_SET: | |
pass | |
def list_out_loop(): | |
for x in STRINGS: | |
if x in AS_LIST: | |
pass | |
times = {} | |
funcs = ("set_in_loop", "list_in_loop", "set_out_loop", "list_out_loop") | |
for func in funcs: | |
print(f"Timing {func}...", end="") | |
duration = timeit(f"{func}()", setup=f"from __main__ import {func}", number=1000000) | |
times[func] = duration | |
print(f" {duration:0.3f}s") | |
min_time = min(times.values()) | |
max_length = max(len(name) for name in times) | |
order = sorted((dur, name) for name, dur in times.items()) | |
ordered_names = [name for dur, name in order] | |
print(f"\n\n{'function':<{max_length}} time factor") | |
for name in ordered_names: | |
duration = times[name] | |
factor = duration / min_time | |
print(f"{name:>{max_length}} {duration:0.3f}s ({factor:0.1f}x)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment