>>
|
No. 183
>>178
I decided to look around and do some tests.
The creation of tuples is much faster than lists, but accessing elements in a list is faster than a tuple.
From the tests I did, 12 elements in a tuple really isnt that many, so accessing really isnt that much of a problem.
The following tests were done using `python -m timeit -n 1000000` on Ubuntu 11.04 with Python 2.6.6
Example test: python -m timeit -n 1000000 "valid = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')" "if 'Jan' in valid: pass"
Tuple -> Jan
1000000 loops, best of 3: 0.105 usec per loop
List -> Jan
1000000 loops, best of 3: 0.388 usec per loop
Tuple -> Feb
1000000 loops, best of 3: 0.133 usec per loop
List -> Feb
1000000 loops, best of 3: 0.414 usec per loop
Tuple -> Mar
1000000 loops, best of 3: 0.165 usec per loop
List -> Mar
1000000 loops, best of 3: 0.44 usec per loop
Tuple -> Apr
1000000 loops, best of 3: 0.175 usec per loop
List -> Apr
1000000 loops, best of 3: 0.455 usec per loop
Tuple -> May
1000000 loops, best of 3: 0.226 usec per loop
List -> May
1000000 loops, best of 3: 0.51 usec per loop
Tuple -> Jun
1000000 loops, best of 3: 0.25 usec per loop
List -> Jun
1000000 loops, best of 3: 0.537 usec per loop
Tuple -> Jul
1000000 loops, best of 3: 0.306 usec per loop
List -> Jul
1000000 loops, best of 3: 0.587 usec per loop
Tuple -> Aug
1000000 loops, best of 3: 0.29 usec per loop
List -> Aug
1000000 loops, best of 3: 0.58 usec per loop
Tuple -> Sep
1000000 loops, best of 3: 0.288 usec per loop
List -> Sep
1000000 loops, best of 3: 0.575 usec per loop
Tuple -> Oct
1000000 loops, best of 3: 0.323 usec per loop
List -> Oct
1000000 loops, best of 3: 0.605 usec per loop
Tuple -> Nov
1000000 loops, best of 3: 0.33 usec per loop
List -> Nov
1000000 loops, best of 3: 0.626 usec per loop
Tuple -> Dec
1000000 loops, best of 3: 0.372 usec per loop
List -> Dec
1000000 loops, best of 3: 0.653 usec per loop
Tuple -> Bad
1000000 loops, best of 3: 0.373 usec per loop
List -> Bad
1000000 loops, best of 3: 0.662 usec per loop
As you can see, when searching for the first element of a list a tuple is about 3 times faster, but once every element has to be accessed, a tuple is only 2 times faster.
That being said, your a/s/l example makes sense to use a tuple over a list, as the position matters, and the list of people makes sense as you are not aware of the number of people and the order doesn't matter, but unless you are writing a small script, to me it would make more sense to use neither and instead make a small asl/person class.
To me, when I see something like (['1', 'Jan', 'January'], ['2', 'Feb', 'Feburary']...), it makes me think that the number of months is set, but the contents of a month may change during runtime, and for OP's example where he has a set of valid inputs to be used to validate user input, I am still not seeing why you would use a list over a tuple, or maybe I just missed your point completely.
|