[Python-3000] Python-3000 Digest, Vol 2, Issue 151
Ian Bicking
ianb at colorstudy.com
Wed Apr 26 18:06:30 CEST 2006
Jim Jewett wrote:
> On 4/26/06, Antoine Pitrou <solipsis at pitrou.net> wrote:
>
>>2) set([1,2,3]) makes little sense anyway, since it
>>probably isn't significantly more efficient than [1,2,3]
>
>
> In the current implementation, is is less efficient.
>
> But a set is what you really intend; using a list simply because it is
> currently more efficient is a bad thing to encourage.
I my not-at-all-scientific tests, sets were faster when the set is
created ahead of time, but just barely, except when you were testing
something that was at the front of the list. Tuples were always a bit
slower than sets or lists.
But if you factor in the expense of actually creating the
set/list/tuple, then the different is more significant; a little less
than a factor of three between tuples and lists (since tuples can be
allocated once), and a little less than a factor of three between lists
and sets when building the set from a list (which is what everyone has
been using as examples), or a little less than a factor of two when
building from a tuple.
I imagine a set literal would improve the set case a little, but since
it hashes its arguments it's always going to have more overhead to
create than a list. If you could create a *frozen* set with a literal,
that should outperform lists and tuples.
But I should note that I didn't do very thorough testing at all, just
tested loops that used "in" on a three-item container (and in py2.4).
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-3000
mailing list