Last active
May 28, 2018 20:24
-
-
Save Amwam/afc8d0cfc614d93ae020cb4d363803be to your computer and use it in GitHub Desktop.
Time iteration of tuples/lists/sets
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
2.7.10 (default, Oct 6 2017, 22:29:07) | |
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] | |
Creating a set | |
using a tuple: 0.689293146133 | |
using a list: 0.676049947739 | |
using a set: 0.665240049362 | |
Creating a tuple | |
using a tuple: 0.450199842453 | |
using a list: 0.442317008972 | |
using a set: 0.475040912628 | |
Creating a list | |
using a tuple: 0.420726060867 | |
using a list: 0.447565078735 | |
using a set: 0.428601980209 |
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
3.6.4 (default, Mar 9 2018, 23:15:03) | |
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] | |
Creating a set | |
using a tuple: 0.6933924070326611 | |
using a list: 0.6775112049654126 | |
using a set: 0.6732040569186211 | |
Creating a tuple | |
using a tuple: 0.47215579298790544 | |
using a list: 0.4833532590419054 | |
using a set: 0.4855397519422695 | |
Creating a list | |
using a tuple: 0.4640890349401161 | |
using a list: 0.48467018199153244 | |
using a set: 0.47771214391104877 |
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
from __future__ import print_function | |
import sys | |
import timeit | |
setup = """ | |
try: | |
range_func = xrange | |
except NameError: | |
range_func = range | |
def generate(cls): | |
return cls(range_func(0, 1000000)) | |
t = generate(tuple) | |
l = generate(list) | |
s = generate(list) | |
""" | |
tries = 10 | |
print(sys.version) | |
print('Creating a set') | |
print('using a tuple: ', timeit.timeit(stmt='set(x for x in t)', number=tries,setup=setup)) | |
print('using a list: ', timeit.timeit(stmt='set(x for x in l)', number=tries, setup=setup)) | |
print('using a set: ', timeit.timeit(stmt='set(x for x in s)', number=tries, setup=setup)) | |
print('') | |
print('Creating a tuple') | |
print('using a tuple: ', timeit.timeit(stmt='tuple(x for x in t)', number=tries,setup=setup)) | |
print('using a list: ', timeit.timeit(stmt='tuple(x for x in l)', number=tries, setup=setup)) | |
print('using a set: ', timeit.timeit(stmt='tuple(x for x in s)', number=tries, setup=setup)) | |
print('') | |
print('Creating a list') | |
print('using a tuple: ', timeit.timeit(stmt='list(x for x in t)', number=tries,setup=setup)) | |
print('using a list: ', timeit.timeit(stmt='list(x for x in l)', number=tries, setup=setup)) | |
print('using a set: ', timeit.timeit(stmt='list(x for x in s)', number=tries, setup=setup)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment