Efficiency: list or tuple?

Bengt Richter bokr at oz.net
Wed Jan 15 19:30:27 EST 2003


On 15 Jan 2003 17:32:25 GMT, Spencer Ernest Doidge <spencer at efn.org> wrote:

>I'm interested in putting pairs of integers and floats into a sequence 
>of indeterminate length.

>I'm collecting this data in real time, and I want to use the most efficient
>available method so as to be able to gather data as fast as possible and
>append it to the sequence as I go.

Well, it may depend on what the source of your data looks like to python.
Is it a C extension that implements a fifo for your data that you can
asynchronously (i.e., low level sync/lock taken care of in C) read from
and get chunks back from? Or are you getting single values and synthesizing
the other half of the pairs as you go? Or what?

And how fast is fast enough? E.g., is this partly an audio sample stream?
Will your whole input fit in memory, or should you be worrying about the
fastest way to move it into a file stream? Or ???

You might want to look at the array module and consider having two
separate homogeneous arrays, one for floats and one for the integers.
You could pre-allocate large arrays, so there's no hiccups in allocation
overhead. If you actually need an i-th tuple, you can always write 
(intarray[i],floatarray[i]) later, since you know values with equal indices
are pairs, if you've made it so.

>As far as I can tell, I have lists and tuples to choose from. 
>Does either of these structures work better in this situation, or is
>there some other way I don't know about?
>
It all depends on where it's coming from and where it's going ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list