Created
November 14, 2010 13:21
-
-
Save lsbardel/676153 to your computer and use it in GitHub Desktop.
Test the three algorithms for iterating on a zipped sliced lists
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 timeit | |
from itertools import izip, islice | |
data = range(0,100000) | |
def loop1(): | |
return zip(data[::2],data[1::2]) | |
def loop2(): | |
return izip(data[::2],data[1::2]) | |
def loop3(): | |
return izip(islice(data,0,None,2),islice(data,1,None,2)) | |
def todict1(): | |
return dict(loop1()) | |
def todict2(): | |
return dict(loop2()) | |
def todict3(): | |
return dict(loop3()) | |
if __name__=='__main__': | |
'''The fastest is loop2. The islice iterator is slower than slicing the list (what is the point of it!!).''' | |
t = timeit.Timer("todict1()", "from __main__ import todict1") | |
print('old loop: %s' % t.timeit(100)) | |
t = timeit.Timer("todict2()", "from __main__ import todict2") | |
print('new loop: %s' % t.timeit(100)) | |
t = timeit.Timer("todict3()", "from __main__ import todict3") | |
print('new loop: %s' % t.timeit(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment