元组与列表 [英] tuples vs lists

查看:70
本文介绍了元组与列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用元组和使用

列表之间是否有任何性能/尺寸差异?

-

-

每一个足够先进的魔法都与技术难以区分

- Arthur C Anticlarke

Are there any performance/size differences between using tuples and using
lists?
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke

推荐答案

2003年9月16日星期二18:51:38 +0200,谣言称Ivan Voras
On Tue, 16 Sep 2003 18:51:38 +0200, rumours say that "Ivan Voras"
<iv****@fer.hr> might have written:
使用元组和使用
列表之间是否有任何性能/大小差异?
Are there any performance/size differences between using tuples and using
lists?




假设你知道元组是不可变的并且列表是可变的(它们可以增长,收缩,排序等等),元组消耗更少的内存(列表

分配比你的内存更多的内存列表需要,以避免在每个附加/插入时重新分配空间

)。这说明了大小。


性能方面,我认为

索引列表和元组之间没有明显的区别(但你总是可以使用timeit模块)。


IIRC Guido已经声明元组不是一成不变的,而是一种方便的无名结构(一个la) C)或记录(a /

Pascal)。


请记住,列表中有有用的方法(如计数和索引)

那个元组不是。关于功能性。

-

TZOTZIOY,我说英格兰最好,

Microsoft安全警报:Matrix开始是开源的。



Assuming you know that tuples are immutable and lists are mutable (they
can grow, shrink, get sorted etc), tuples consume less memory (lists
allocate more memory than your list needs, to avoid reallocating space
on every append/insert). This accounts for size.

Performance-wise, I don''t think there is a noticable difference between
indexing lists and tuples (but you can always use the timeit module).

IIRC Guido has stated that tuples are not intended to be immutable
lists, but a kind of handy unnamed structs (a la C) or records (a la
Pascal).

Keep in mind also that lists have useful methods (like count and index)
that tuples don''t. This about functionality.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.


好的,我收集的应该是更小的& amp;我认为更快:)

-

-

每一个足够先进的魔法都与技术难以区分

--Arthur C Anticlarke
Ok, I gather touples should be smaller & faster as I thought :)
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke


" Ivan Voras" < 4 **** @ fer.hr>写道:
"Ivan Voras" <iv****@fer.hr> writes:
使用元组和使用
列表之间是否存在任何性能/大小差异?
Are there any performance/size differences between using tuples and using
lists?




我制作创建/解包/索引的时间简短

元组/列表和循环用于更长的元组/列表。在Python 2.1,2.2和

2.3中,短元组的创建和解包速度快2倍/ 3倍,这可能是因为知道它们是固定大小,但是索引是

元组稍微慢一些,我没想到。无论你在元组还是列表上循环

都只对2.2(元组慢30%)很重要。

如果存在2倍差异或30%差异,则可以在

后续运行下面给出的脚本。

lupan @ psi:[〜/ dyplom / src]



I made the timings of creation/unpacking/indexing for short
tuples/lists and loops for longer tuples/lists. In Python 2.1, 2.2 and
2.3 creation and unpacking of short tuples is 2x/3x faster which is
what you may expact knowing they are fixed size, but indexing is
slightly slower for tuples which I didn''t expect. Whether you loop
over tuple or list does matter only for 2.2 (tuples are 30% slower).
Where there is 2x difference or 30% difference it is reproducable in
subsequent runs of of the script given below.
lupan@psi:[~/dyplom/src]


这篇关于元组与列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆