Created
December 15, 2009 16:38
-
-
Save lqc/257068 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
''' | |
Created on 2009-12-15 | |
@author: lreqc | |
''' | |
import timeit | |
SETUP = """\ | |
import random | |
alphabet = ['a', 'b', 'c', ' '] | |
rng = random.Random() | |
charlist = [ rng.choice(alphabet) for _ in xrange(1000) ] | |
""" | |
GROUPBY_LAMBDA = """ | |
import itertools | |
def list_split(iterable): | |
return [''.join(slowo) for jest, slowo in itertools.groupby(iterable, lambda x: bool(x.strip())) if jest] | |
""" | |
GROUPBY_PLAIN = """ | |
import itertools | |
def list_split(iterable): | |
return [''.join(word) for ommit, word in itertools.groupby(iterable, str.isspace) if not ommit] | |
""" | |
FORLOOP = """ | |
def list_split(lista): | |
slowa = [] | |
aktualne_slowo = '' | |
for litera in lista: | |
if litera.isspace(): | |
if aktualne_slowo: | |
slowa.append(aktualne_slowo) | |
aktualne_slowo = '' | |
else: | |
aktualne_slowo += litera | |
if aktualne_slowo: | |
slowa.append(aktualne_slowo) | |
return slowa | |
""" | |
if __name__ == '__main__': | |
print "Groupby with lambda (average time from 10 runs, 500 calls per):" | |
print sum(timeit.repeat("list_split(charlist)", setup = SETUP + GROUPBY_LAMBDA, number = 500, repeat = 10)) / 10 | |
print "Groupby with builtin:" | |
print sum(timeit.repeat("list_split(charlist)", setup = SETUP + GROUPBY_PLAIN, number = 500, repeat = 10)) / 10 | |
print "For loop with builtins and lists:" | |
print sum(timeit.repeat("list_split(charlist)", setup = SETUP + FORLOOP, number = 500, repeat = 10)) / 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment