Last active
May 19, 2022 12:10
-
-
Save Ricks-Lab/82ee51d7d0457f0f8457f3598fa8c57b to your computer and use it in GitHub Desktop.
String Search in a list/tuple/set of Strings
This file contains 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
#!/usr/bin/python3 | |
import timeit | |
import gc | |
gc.disable() | |
test_list = ['rick', 'junk', 'fred', 'exclamation point', 'fried', 'rickster', 'john', 'johnathan', 'nathan', 'a', 'b', 'r'] | |
test_set = {'rick', 'junk', 'fred', 'exclamation point', 'fried', 'rickster', 'john', 'johnathan', 'nathan', 'a', 'b', 'r'} | |
test_tup = ('rick', 'junk', 'fred', 'exclamation point', 'fried', 'rickster', 'john', 'johnathan', 'nathan', 'a', 'b', 'r') | |
def search_in_set(): | |
for test_word in ['rick', 'flib', 'fried', 'x', 'r']: | |
if test_word in test_set: pass | |
def search_in_list(): | |
for test_word in ['rick', 'flib', 'fried', 'x', 'r']: | |
if test_word in test_list: pass | |
def search_in_tup(): | |
for test_word in ['rick', 'flib', 'fried', 'x', 'r']: | |
if test_word in test_tup: pass | |
print('Search in List time: {}'.format(timeit.timeit(search_in_list))) | |
print('Search in Tuple time: {}'.format(timeit.timeit(search_in_tup))) | |
print('Search in Set time: {}'.format(timeit.timeit(search_in_set))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With garbage collection:
Without garbage collection: