项目访问时间:设置v。列表 [英] item access time: sets v. lists

查看:51
本文介绍了项目访问时间:设置v。列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对访问set元素的期望是否比访问list元素要慢得多?b $ b?说明?

谢谢,

Alan Isaac


>> t1 = timeit.Timer(&for; for i in set(xrange(10000)):pass","")
t2 = timeit.Timer(" for i in list(xrange(10000)):pass","")
t1.timeit(1000)



9.806250235714316


>> t2.timeit(1000)



3.9823075279120701

解决方案

2006年10月4日星期三16:02:56 GMT

David Isaac < ai ***** @ verizon.netwrote:


预期对设置元素的访问要比

慢吗?访问列表元素?说明?

谢谢,

Alan Isaac


> t1 = timeit.Timer(" for i in set(xrange(10000)):pass,,)
t2 = timeit.Timer(" for i in list(xrange(10000)):pass" ,")



[...]



你''重新测量创建xrange和set / list的时间。

在调用Timer()之前创建它们并重复你的时间。

Dennis Benzinger


" David Isaac" < ai ***** @ verizon.netwrote in message

news:QWQUg.15396


3T2.2982@trnddc06 ...


访问set元素是否比访问list元素要慢得多?b $ b?说明?

谢谢,

Alan Isaac


>> > t1 = timeit.Timer(&for; for i in set(xrange(10000)):pass","")
t2 = timeit.Timer(" for i in list(xrange(10000)) ):传递","")
t1.timeit(1000)



9.806250235714316


>>> t2.timeit(1000)



3.9823075279120701



几点:

1.你对timeit的使用有点瑕疵。您不仅可以将访问权限计入集合或列表,还可以构建集合/列表,*每个

时间*。使用第二个字符串arg(你要传递的那个作为")来进行一次性初始化,这样你只能测量访问权限,这就是你的b
说你有兴趣。

2.哎呀,事实证明你并没有真正*访问*设置/列表,你是

*迭代*在它上面。鉴于这些集合是使用

树结构在内部实现的,我希望迭代列表可能会更快,

尽管只是略微如此。

3.以下是一些更正确的时间统计数据:


构造列表/设置

设置-1.89524618352

清单-0.299499796762

迭代列表/设置

set -0.646887523414

list -0.552001162159


随机访问列表/集中的第一项

set -0.000189409547861

list -0.000160076210804


随机访问项目存在时列表/设置项目

set -0.000241650824337

list -0.0245168031132


随机访问列表/集合中的项目当项目不存在时

set -0.000187733357172

list -0.522086186932

代码:

import timeit


print" \\\
Construct list / set"

t1 = timeit.Timer(" z = set(xrange(10000))","")

t2 = timeit.Timer(" z = list(xrange(10000))","")

print" set - >",t1.timeit(1000)

print" list - >",t2.timeit(1000)


print" \\\
Iterate over list / set"

t1 = timeit.Timer(" for i in z:pass"," z = set (xrange(10000))")

t2 = timeit.Timer(" for i in z:pass"," z = list(xrange(10000))")

print" set - >",t1.timeit(1000)

print" list - >",t2.timeit(1000)


print" \ nandand访问list / set中的第一项"

t1 = timeit.Timer(" 0 in z"," z = set(xrange) (10000))")

t2 = timeit.Timer(" 0 in z"," z = list(xrange(10000))")

print" set - >",t1.timeit(1000)

print" list - >",t2.timeit(1000)


打印" \ nR当项目存在时,andom访问列表/集合中的项目

t1 = timeit.Timer(" 500 in z"," z = set(xrange(10000))")

t2 = timeit.Timer(" 500 in z"," z = list(xrange(10000))")

print" set - >", t1.timeit(1000)

打印" list - >",t2.timeit(1000)


print" \ nandand访问权限列表中的项目/当项目不存在时设置

t1 = timeit.Timer(" 10500 in z,z = set(xrange(10000)))

t2 = timeit.Timer(" 10500 in z"," z = list(xrange(10000))")

print" set - >",t1 .timeit(1000)

打印" list - >",t2.timeit(1000)

- Paul


Is it expected for access to set elements to be much
slower than access to list elements? Explanation?
Thanks,
Alan Isaac

>>t1=timeit.Timer("for i in set(xrange(10000)):pass","")
t2=timeit.Timer("for i in list(xrange(10000)):pass","")
t1.timeit(1000)

9.806250235714316

>>t2.timeit(1000)

3.9823075279120701

解决方案

On Wed, 04 Oct 2006 16:02:56 GMT
"David Isaac" <ai*****@verizon.netwrote:

Is it expected for access to set elements to be much
slower than access to list elements? Explanation?
Thanks,
Alan Isaac

>t1=timeit.Timer("for i in set(xrange(10000)):pass","")
t2=timeit.Timer("for i in list(xrange(10000)):pass","")

[...]

You''re measuring the time for creating the xrange and the set/list too.
Create them before you call Timer() and repeat your timing.
Dennis Benzinger


"David Isaac" <ai*****@verizon.netwrote in message
news:QWQUg.15396


3T2.2982@trnddc06...

Is it expected for access to set elements to be much
slower than access to list elements? Explanation?
Thanks,
Alan Isaac

>>>t1=timeit.Timer("for i in set(xrange(10000)):pass","")
t2=timeit.Timer("for i in list(xrange(10000)):pass","")
t1.timeit(1000)

9.806250235714316

>>>t2.timeit(1000)

3.9823075279120701

A couple of points:
1. Your use of timeit is a bit flawed. You are not only timing the access
into the set or list, but also the construction of the set/list, *every
time*. Use the second string arg (the one you are passing as "") for
one-time initialization, so that you measure only access, which is what you
say your are interested in.
2. Ooops, it turns out you''re not really *accessing* the set/list, you are
*iterating* over it. Given that sets are implemented internally using a
tree structure, I would expect that iterating over a list could be faster,
although only marginally so.
3. Here are some stats for a little more correct timeits:

Construct list/set
set -1.89524618352
list -0.299499796762

Iterate over list/set
set -0.646887523414
list -0.552001162159

Random access to first item in list/set
set -0.000189409547861
list -0.000160076210804

Random access to item in list/set when item exists
set -0.000241650824337
list -0.0245168031132

Random access to item in list/set when item does not exist
set -0.000187733357172
list -0.522086186932
The code:
import timeit

print "\nConstruct list/set"
t1=timeit.Timer("z = set(xrange(10000))","")
t2=timeit.Timer("z = list(xrange(10000))","")
print "set ->", t1.timeit(1000)
print "list ->", t2.timeit(1000)

print "\nIterate over list/set"
t1=timeit.Timer("for i in z: pass","z = set(xrange(10000))")
t2=timeit.Timer("for i in z: pass", "z = list(xrange(10000))")
print "set ->", t1.timeit(1000)
print "list ->", t2.timeit(1000)

print "\nRandom access to first item in list/set"
t1=timeit.Timer("0 in z","z = set(xrange(10000))")
t2=timeit.Timer("0 in z", "z = list(xrange(10000))")
print "set ->", t1.timeit(1000)
print "list ->", t2.timeit(1000)

print "\nRandom access to item in list/set when item exists"
t1=timeit.Timer("500 in z","z = set(xrange(10000))")
t2=timeit.Timer("500 in z", "z = list(xrange(10000))")
print "set ->", t1.timeit(1000)
print "list ->", t2.timeit(1000)

print "\nRandom access to item in list/set when item does not exist"
t1=timeit.Timer("10500 in z","z = set(xrange(10000))")
t2=timeit.Timer("10500 in z", "z = list(xrange(10000))")
print "set ->", t1.timeit(1000)
print "list ->", t2.timeit(1000)
-- Paul


这篇关于项目访问时间:设置v。列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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