冷冻问题 [英] frozenset question

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

问题描述




使用冷冻套装除了它之外还有什么好处

是不可变的?

Will McGugan

-
http://www.willmcgugan .com

"" .join({''*'':''@'',''''''''。''}。get(c ,0)或chr(97+(ord(c)-84)%26)for c in

" jvyy * jvyyzpthtna ^ pbz")

Hi,

Are there any benefits in using a frozenset over a set, other than it
being immutable?
Will McGugan
--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")

推荐答案

2005年7月6日,Will McGugan< ne ** @ nowillmcguganspam.com>写道:
On 7/6/05, Will McGugan <ne**@nowillmcguganspam.com> wrote:


除了它是不可变的,在集合中使用冷冻集有什么好处吗?
Hi,

Are there any benefits in using a frozenset over a set, other than it
being immutable?




冻结集可以用作词典的关键:


..>> s1 = set([1])

..>> s2 = frozenset([2])

..>> {s1:1}

回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:set对象不可用

..>> {s2:1}

{frozenset([2]):1}


-

Qiangning Hong
获取Firefox! < http://www.spreadfirefox.com/?q = affiliates& amp; id = 67907& amp; t = 1>



A frozenset can be used as a key of a dict:

..>> s1 = set([1])
..>> s2 = frozenset([2])
..>> {s1: 1}
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: set objects are unhashable
..>> {s2:1}
{frozenset([2]): 1}

--
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&amp;id=67907&amp;t=1>


Qiangning Hong写道:
Qiangning Hong wrote:
2005年7月6日,Will McGugan< ne ** @ nowillmcguganspam.com>写道:
On 7/6/05, Will McGugan <ne**@nowillmcguganspam.com> wrote:


除了它是不可变的之外,在集合中使用冷冻集有什么好处吗?
Hi,

Are there any benefits in using a frozenset over a set, other than it
being immutable?



冻结集可以用作字典的键:


A frozenset can be used as a key of a dict:




谢谢,但我的意思是暗示。


我想知道冷冻设备在某种程度上是更快还是更有效。

回想C ++的黑暗时代,你可以优化

你知道不变。




-
http://www.willmcgugan.com

"" .join({''*'':'''''' ,''''':''。''}。得到(c,0)或chr(97+(ord(c)-84)%26)对于c来说,

" jvyy * jvyyzpthtna ^ pbz")



Thanks, but I meant to imply that.

I was wondering if frozenset was faster or more efficient in some way.
Thinking back to the dark ages of C++, you could optimize things that
you knew to be constant.
Will

--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")


2005年7月6日星期三11:30:14 +0100,Will McGugan写道:
On Wed, 06 Jul 2005 11:30:14 +0100, Will McGugan wrote:
我想知道冷冻组是否更快或更有效率回想一下C ++的黑暗时代,你可以优化你知道的不变的东西。
I was wondering if frozenset was faster or more efficient in some way.

Thinking back to the dark ages of C++, you could optimize things that
you knew to be constant.




你为什么要这样做?


py>进口套装

py>进口时间

py> bigset = sets.Set(范围(500000))

py> bigimmutableset = sets.ImmutableSet(range(500000))

py>断言len(bigset)== len(bigimmutableset)

py>

py> def测试器(S,L):

...."""测试L中的物品是否在S中,并为过程计时。""

.... t = time.time()

.... for i in range(100):

.... for item in L:

....项目在S

....返回time.time() - t#返回的时间是100循环

....

py>


时间成功测试:


py>测试员(bigset,范围(100,500))

0.11539506912231445

py>测试仪(bigimmutableset,范围(100,500))

0.12014198303222656

在进行100 * 400检查是否为整数时几乎没有区别

在集合中。但是,让我们再试一次,以防万一:


py>测试员(bigset,范围(100,500))

0.10998892784118652

py>测试仪(bigimmutableset,范围(100,500))

0.11114096641540527

差异无关紧要。不成功的支票怎么样?


py>测试员(bigset,范围(-100,-500,-1))

0.12070298194885254

py>测试员(bigset,范围(-100,-500,-1))

0.11681413650512695

py> tester(bigimmutableset,range(-100,-500,-1))

0.11313891410827637

py>测试器(bigimmutableset,范围(-100,-500,-1))

0.11315703392028809

不可变和可变之间没有明显的速度差异
设置,至少用于查询。无论是成功还是不成功,可变或不可变,每次测试中的项目都需要大约0.0000025秒才能完成

。为什么你需要对它进行优化?


如果你告诉我们你想要做什么,在什么情况下呢?b $ b太慢了,我们'我们会看看我们是否可以建议一些方法来优化它。


但是如果你只是为了优化而试图进行优化,那么'/ b $ b''一个可怕的主意。让您的计划先行。然后当它b $ b工作时,测量它的运行速度。如果,而且只是,如果它太慢,

识别程序的部分,使其太慢。这意味着

分析和时间。然后优化那些零件,没有别的。


否则,你会像汽车设计师一样试图通过使安全带空气动力学来加速他的运动。 。

-

史蒂文。



Why would you want to?

py> import sets
py> import time
py> bigset = sets.Set(range(500000))
py> bigimmutableset = sets.ImmutableSet(range(500000))
py> assert len(bigset) == len(bigimmutableset)
py>
py> def tester(S, L):
.... """Test if items from L are in S, and time the process."""
.... t = time.time()
.... for i in range(100):
.... for item in L:
.... item in S
.... return time.time() - t # time returned is for 100 loops
....
py>

Time some successful tests:

py> tester(bigset, range(100, 500))
0.11539506912231445
py> tester(bigimmutableset, range(100, 500))
0.12014198303222656

Practically no difference when doing 100*400 checks of whether an integer
is in the set. But let''s try again, just in case:

py> tester(bigset, range(100, 500))
0.10998892784118652
py> tester(bigimmutableset, range(100, 500))
0.11114096641540527

The difference is insignificant. How about unsuccessful checks?

py> tester(bigset, range(-100, -500, -1))
0.12070298194885254
py> tester(bigset, range(-100, -500, -1))
0.11681413650512695
py> tester(bigimmutableset, range(-100, -500, -1))
0.11313891410827637
py> tester(bigimmutableset, range(-100, -500, -1))
0.11315703392028809

There is no significant speed difference between immutable and mutable
sets, at least for queries. Regardless of whether it is successful or
unsuccessful, mutable or immutable, it takes about 0.0000025 second to do
each test of item in set. Why would you need to optimize that?

If you tell us what you are trying to do, and under what circumstances it
is too slow, we''ll see if we can suggest some ways to optimize it.

But if you are just trying to optimize for the sake of optimization,
that''s a terrible idea. Get your program working first. Then when it
works, measure how fast it runs. If, and ONLY if, it is too slow,
identify the parts of the program that make it too slow. That means
profiling and timing. Then optimize those parts, and nothing else.

Otherwise, you will be like the car designer trying to speed up his sports
cars by making the seatbelts aerodynamic.
--
Steven.


这篇关于冷冻问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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