Created
May 14, 2017 17:40
-
-
Save josemoralesp/723f84796694d3222c046f027ed8d32f to your computer and use it in GitHub Desktop.
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
import itertools | |
import timeit | |
def using_list_obj(): | |
tok = 'Casa' | |
toks = ['Cosa', 'Nada', 'Por', 'Camion', 'Ejto'] | |
val = list(i for i in toks if i[0] == tok[0]) | |
return [i for i in val] # Sorry only for test | |
def using_list_com(): | |
tok = 'Casa' | |
toks = ['Cosa', 'Nada', 'Por', 'Camion', 'Ejto'] | |
val = [i for i in toks if i[0] == tok[0]] | |
return [i for i in val] # Sorry only for test | |
def using_itertools(): | |
tok = 'Casa' | |
toks = ['Cosa', 'Nada', 'Por', 'Camion', 'Ejto'] | |
val = list( | |
itertools.takewhile(lambda other_token, _token=tok: | |
other_token[0] == _token[0], toks)) | |
return [i for i in val] # Sorry only for test | |
def using_itertools_com(): | |
tok = 'Casa' | |
toks = ['Cosa', 'Nada', 'Por', 'Camion', 'Ejto'] | |
val = [ | |
itertools.takewhile(lambda other_token, _token=tok: | |
other_token[0] == _token[0], toks)] | |
return [i for i in val] # Sorry only for test | |
def using_itertools_without_list(): | |
tok = 'Casa' | |
toks = ['Cosa', 'Nada', 'Por', 'Camion', 'Ejto'] | |
val = itertools.takewhile(lambda other_token, _token=tok: | |
other_token[0] == _token[0], toks) | |
return [i for i in val] # Sorry only for test | |
print 'using_list_obj', using_list_obj() | |
print 'using_list_com', using_list_com() | |
print 'using_itertools', using_itertools() | |
print 'using_itertools_com', using_itertools_com() | |
print 'using_itertools_without_list', using_itertools_without_list() | |
print 'using_list_obj', timeit.Timer(using_list_obj).timeit() | |
print 'using_list_com', timeit.Timer(using_list_com).timeit() | |
print 'using_itertools', timeit.Timer(using_itertools).timeit() | |
print 'using_itertools_com', timeit.Timer(using_itertools_com).timeit() | |
print 'using_itertools_without_list', timeit.Timer( | |
using_itertools_without_list).timeit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment