Created
July 12, 2018 00:26
-
-
Save davidwtbuxton/29ad6346f868c86ad26fcaab76dcbdd4 to your computer and use it in GitHub Desktop.
In Python, which is fastest to build? A literal list, tuple or set?
This file contains hidden or 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
$ python3.6 -m timeit '[20, 50, 75, 100]' | |
10000000 loops, best of 3: 0.0618 usec per loop | |
$ python3.6 -m timeit '(20, 50, 75, 100)' | |
100000000 loops, best of 3: 0.0132 usec per loop | |
$ python3.6 -m timeit '{20, 50, 75, 100}' | |
10000000 loops, best of 3: 0.127 usec per loop | |
$ python2.7 -m timeit '[20, 50, 75, 100]' | |
10000000 loops, best of 3: 0.126 usec per loop | |
$ python2.7 -m timeit '(20, 50, 75, 100)' | |
100000000 loops, best of 3: 0.0134 usec per loop | |
$ python2.7 -m timeit '{20, 50, 75, 100}' | |
10000000 loops, best of 3: 0.116 usec per loop | |
$ python3.6 --version | |
Python 3.6.5 | |
$ python2.7 --version | |
Python 2.7.15 | |
# Answer: for 4 integers, fastest is a tuple. A lot faster if you are using Python 2.7. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment