tuples, index method, Python's design

Michael Zawrotny zawrotny at sb.fsu.edu
Wed Apr 11 16:21:49 EDT 2007


On Wed, 11 Apr 2007 10:57:19 -0500, Chris Mellon <arkanes at gmail.com> wrote:
> 
>  The primary use case for index on tuple is because people use them as
>  immutable lists. That's fine as far as it goes, but I want to know
>  what the justification is for using an immutable list, and if you have
>  one why you need to use index() on it. There's only 2 use cases I've
>  heard so far for that: certain types of binary parsing, which I don't
>  think is common enough to justify modification to the language core,
>  and third party libs returning silly things, which I *certainly* don't
>  think justifies changes to the language core.

In regard to unpacking binary data and similar uses, those are
often perfomance sensitive, here is some benchmark data from
timeit.Timer on looking for an item in a list/tuple via list.index() or
list(tuple).index().  The best case scenario (short list) takes about a
20% performance hit.

                        run time (sec) 
size       runs       list         tuple        ratio (t/l) 
1000000    100        1.997614     5.526909     2.766755    
100000     1000       2.049710     5.291704     2.581684    
10000      10000      1.970400     2.714083     1.377428    
1000       100000     2.325089     3.013624     1.296133    
100        1000000    6.213748     7.661165     1.232938    
10         10000000   44.970536    53.685698    1.193797    

For whatever it's worth, I'm mildly in favor of index() and count() being 
added to tuples.


Mike

-- 
Michael Zawrotny
Institute of Molecular Biophysics
Florida State University                | email:  zawrotny at sb.fsu.edu
Tallahassee, FL 32306-4380              | phone:  (850) 644-0069



More information about the Python-list mailing list