Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Another (admittedly trivial) experiment with speed and storage: Iterating through a 10,000 element list was about 2% slower than iterating through a 10,000 element tuple, and the list was 16 bytes bigger than the tuple:

  #!/usr/bin/python

  from sys import getsizeof
  from random import sample
  from time import time

  l = sample(xrange(10000), 10000)
  t = tuple(l)

  print "The tuple's size is " + str(getsizeof(t)) + " and the list's size is " + str(getsizeof(l))

  s = time()
  z = 0
  for x in l:
    z += x

  print "Iterating through a 10,000 element list took " + str(time() - s) + " seconds."

  s = time()
  z = 0
  for x in t:
    z += x

  print "Iterating through a 10,000 element tuple took " + str(time() - s) + " seconds."


  doug@supermicro:~$ ./x
  The tuple's size is 80056 and the list's size is 80072 
  Iterating through a 10,000 element list took 0.00589299201965 seconds.
  Iterating through a 10,000 element tuple took 0.00538778305054 seconds.

  doug@supermicro:~$ ./x
  The tuple's size is 80056 and the list's size is 80072
  Iterating through a 10,000 element list took 0.00553607940674 seconds.
  Iterating through a 10,000 element tuple took 0.00536203384399 seconds.



This code seems inconclusive -- my first three runs showed that lists are the same, slower, AND faster! (updated for Python 3.5.2 on Windows)

        C:\Temp>python listtuple.py
	The tuple's size is 80048 and the list's size is 80064
	Iterating through a 10,000 element list took 0.0005006790161132812 seconds.
	Iterating through a 10,000 element tuple took 0.0005006790161132812 seconds.

        C:\Temp>python listtuple.py
	The tuple's size is 80048 and the list's size is 80064
	Iterating through a 10,000 element list took 0.0020074844360351562 seconds.
	Iterating through a 10,000 element tuple took 0.001031637191772461 seconds.

        C:\Temp>python listtuple.py
	The tuple's size is 80048 and the list's size is 80064
	Iterating through a 10,000 element list took 0.0 seconds.
	Iterating through a 10,000 element tuple took 0.001001596450805664 seconds.


Interesting! The previous example I posted was Python 2.7.12 on Ubuntu 16.04.01. Here is OSX 10.11.5 w/Python 2.7.10 (also inconclusive with respect to speed, but OSX was half the memory):

  mb:~ doug$ ./listtuple 
  The tuple's size is 40028 and the list's size is 40036
  Iterating through a 10,000 element list took 0.00113916397095 seconds.
  Iterating through a 10,000 element tuple took 0.00111603736877 seconds.
  mb:~ doug$ ./listtuple 
  The tuple's size is 40028 and the list's size is 40036
  Iterating through a 10,000 element list took 0.00126791000366 seconds.
  Iterating through a 10,000 element tuple took 0.00133585929871 seconds.
  mb:~ doug$ ./listtuple 
  The tuple's size is 40028 and the list's size is 40036
  Iterating through a 10,000 element list took 0.00114607810974 seconds.
  Iterating through a 10,000 element tuple took 0.00117492675781 seconds.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: