Created
January 20, 2020 22:46
-
-
Save mikeckennedy/fc491bd359e2f490af9da8d4064957d7 to your computer and use it in GitHub Desktop.
Compare the speed of a complex multiple if test and set containment replement.
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
import datetime | |
def main(): | |
count = 1000 | |
run_sets(count) | |
run_ifs(count) | |
def run_sets(times): | |
t0 = datetime.datetime.now() | |
data = list(range(1, 21)) | |
for n in range(0, times): | |
if n in set(data): | |
pass | |
dt = datetime.datetime.now() - t0 | |
print(f"Sets done in {dt.total_seconds() * 1000:,} ms.") | |
def run_ifs(times): | |
t0 = datetime.datetime.now() | |
for n in range(0, times): | |
if (n == 1 or n == 2 or n == 3 or n == 4 or n == 5 or n == 6 or n == 7 or n == 8 or n == 9 or n == 10 | |
or n == 11 or n == 12 or n == 13 or n == 14 or n == 15 or n == 16 or n == 17 or n == 18 or n == 19 or n == 20): | |
pass | |
dt = datetime.datetime.now() - t0 | |
print(f"Ifs done in {dt.total_seconds() * 1000:,} ms.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I understand - the use of
timeit
in the notebook I've linked to is done for the same reasons.As shown in the example I linked, the time difference is negligible. In addition, if you take into account the time it takes to write or maintain the
x==y
approach, the use ofset()
becomes even more appealing.Thanks again for your time, much appreciated.