Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Created July 12, 2018 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwtbuxton/29ad6346f868c86ad26fcaab76dcbdd4 to your computer and use it in GitHub Desktop.
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?
$ 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