时间差异:插入与追加&相反 [英] Timing Difference: insert vs. append & reverse

查看:60
本文介绍了时间差异:插入与追加&相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我尝试了下面的测试程序。我的兴趣是检查插入与附加和&之间的时间差异。反向列表。我的结果
我的XP Python 2.3.4上的
如下:

time_reverse 0.889999389648

time_insert 15.7750005722

结束多次运行...插入列表头部所需的时间,

与追加到列表然后反转它所需的时间是

通常为16或者是17倍。

我本来期望插入操作比

组合附加和&逆向操作。这种行为是否令人惊讶,或者

是否有充分理由说明存在这种巨大的性能差异?

谢谢,

John


从时间导入时间


def test():

time_reverse = 0.0

time_insert = 0.0

$ x $ b for lindx in xrange(100):

tmp1 = []

time_reverse - = time()

for indx in xrange(10000):

tmp1.append(indx)

tmp1.reverse()

time_reverse + =时间()


tmp2 = []

time_insert - = time()
对于xrange(10000)中的indx,


tmp2.insert(0,indx)

time_insert + = time()

断言tmp1 == tmp2

print" time_reverse",time_reverse

print" time_insert",time_insert


test()

Dear all,
I tried the test program below. My interest is to examine timing
differences between insert vs. append & reverse for a list. My results
on my XP Python 2.3.4 are as follows:
time_reverse 0.889999389648
time_insert 15.7750005722
Over multiple runs ... the time taken to insert at the head of a list,
vs. the time taken to append to a list and then reverse it is
typically 16 or 17 times longer.
I would have expected the insert operation to be faster than the
combined append & reverse operations. Is this behaviour surprising, or
is there a good reason why there is this large performance difference?
Thanks,
John

from time import time

def test():
time_reverse =0.0
time_insert =0.0

for lindx in xrange(100):
tmp1 =[]
time_reverse -=time()
for indx in xrange(10000):
tmp1.append(indx)
tmp1.reverse()
time_reverse +=time()

tmp2 =[]
time_insert -=time()
for indx in xrange(10000):
tmp2.insert(0, indx)
time_insert +=time()
assert tmp1==tmp2
print "time_reverse ", time_reverse
print "time_insert ", time_insert

test()

推荐答案

jo**********@yahoo.com (约翰基林)在

新闻中写道:35 ***** *********************@posting.google.c om:
jo**********@yahoo.com (John Keeling) wrote in
news:35**************************@posting.google.c om:
我尝试了下面的测试程序。我的兴趣是检查插入与附加和&之间的时间差异。反向列表。我在XP Python 2.3.4上的结果如下:
time_reverse 0.889999389648
time_insert 15.7750005722
多次运行...插入列表头部所需的时间,
与追加到列表然后反转它所需的时间相比通常要长16或17倍。
我原本期望插入操作比
combined append&逆向操作。这种行为是否令人惊讶,或者
是否有充分理由说明存在这种巨大的性能差异?
I tried the test program below. My interest is to examine timing
differences between insert vs. append & reverse for a list. My results
on my XP Python 2.3.4 are as follows:
time_reverse 0.889999389648
time_insert 15.7750005722
Over multiple runs ... the time taken to insert at the head of a list,
vs. the time taken to append to a list and then reverse it is
typically 16 or 17 times longer.
I would have expected the insert operation to be faster than the
combined append & reverse operations. Is this behaviour surprising, or
is there a good reason why there is this large performance difference?




列表是通过指向数组的指针实现的它包含的对象。


每次调用''insert(0,indx)''时,

列表中的所有指针都必须是在新的插入之前向上移动一次位置

开头。


当你调用''append(indx)''时,指针只有要复制,如果有
在当前分配的新元素块中没有足够的空间。如果

有空格则无需复制现有元素,只需将
放在最后并更新长度字段。每当一个新的

块必须被分配时,特定追加将不会比插入更快

,但是如果你做的话,将分配一些额外的空间。 />
希望进一步扩展列表。


如果你希望插入更快,也许你认为Python使用了

链接 - 列表实现。它没有这样做,因为在实践中(对于大多数应用程序来说)基于列表的实现提供了更好的性能。



A list is implemented by an array of pointers to the objects it contains.

Every time you call ''insert(0, indx)'', all of the pointers already in the
list have to be moved up once position before the new one can be inserted
at the beginning.

When you call ''append(indx)'' the pointers only have to be copied if there
isn''t enough space in the currently allocated block for the new element. If
there is space then there is no need to copy the existing elements, just
put the new element on the end and update the length field. Whenever a new
block does have to be allocated that particular append will be no faster
than an insert, but some extra space will be allocated just in case you do
wish to extend the list further.

If you expected insert to be faster, perhaps you thought that Python used a
linked-list implementation. It doesn''t do this, because in practice (for
most applications) a list based implementation gives better performance.


John Keeling写道:
John Keeling wrote:
我尝试了下面的测试程序。我的兴趣是检查插入与附加和&之间的时间差异。反向列表。
I tried the test program below. My interest is to examine timing
differences between insert vs. append & reverse for a list.




但你测量的是

一个特定插入(索引为0)和

两个其他内置运算符的组合(在这个

特定情况下)具有相同的效果。


一个好点,最后

我们应该高兴的是,当我们找到方法来加速这些特定情况时,我们不会感到惊讶。


Istvan。



But what you are measuring is the difference between
one particular insert (where the index is 0) and the
combination of two other builtin operators that (in this
particular case) have the same effect.

While your observation makes a good point, in the end
we should pleased not surprised when we find ways to
speed up these specific cases.

Istvan.




" John Keeling" <乔********** @ yahoo.com>在消息中写道

news:35 ************************** @ posting.google.c om ...

"John Keeling" <jo**********@yahoo.com> wrote in message
news:35**************************@posting.google.c om...
亲爱的,
我尝试了下面的测试程序。我的兴趣是检查插入与附加和&之间的时间差异。反向列表。我在XP Python 2.3.4上的结果如下:
time_reverse 0.889999389648
time_insert 15.7750005722
多次运行...插入列表头部所需的时间,
与追加到列表然后反转它所需的时间相比通常要长16或17倍。
Dear all,
I tried the test program below. My interest is to examine timing
differences between insert vs. append & reverse for a list. My results
on my XP Python 2.3.4 are as follows:
time_reverse 0.889999389648
time_insert 15.7750005722
Over multiple runs ... the time taken to insert at the head of a list,
vs. the time taken to append to a list and then reverse it is
typically 16 or 17 times longer.




不应该''这真的是插入与反向&附加&相反。 :-)


Duncan



Shouldn''t that really be insert vs. reverse & append & reverse. :-)

Duncan


这篇关于时间差异:插入与追加&amp;相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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