473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

frozenset question

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")
Jul 21 '05 #1
10 4014
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:

..>> 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>
Jul 21 '05 #2
Qiangning Hong wrote:
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:


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")
Jul 21 '05 #3
On Wed, 06 Jul 2005 11:30:14 +0100, Will McGugan wrote:
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.


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.
Jul 21 '05 #4
Steven D'Aprano wrote:
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.


No need for the 'premature optimization is the root of all evil' speech.
I'm not trying to optimize anything - just enquiring about the nature of
frozenset. If typing 'frozenset' over 'set' gave me a saving in time or
memory (even if tiny) I would favour immutable sets, where appropriate.

Thanks for the info.

Will

--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Jul 21 '05 #5
Will McGugan <ne**@NOwillmcguganSPAM.com> writes:
Qiangning Hong wrote:
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:


Thanks, but I meant to imply that.

I was wondering if frozenset was faster or more efficient in some
way.


No, the 'usable as a dict key' is the main motivation for frozenset's
existence.

Cheers,
mwh

--
The bottom tier is what a certain class of wanker would call
"business objects" ... -- Greg Ward, 9 Dec 1999
Jul 21 '05 #6
Will McGugan wrote:
Are there any benefits in using a frozenset over a set, other than it
being immutable?


No. The underlying implementation is identical with set. The only
difference is the addition of a hash method and absence of mutating
methods. Everything else is the same.
Raymond

Jul 21 '05 #7
On Wed, 06 Jul 2005 14:25:30 +0100, Will McGugan wrote:
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.


No need for the 'premature optimization is the root of all evil' speech.
I'm not trying to optimize anything - just enquiring about the nature of
frozenset. If typing 'frozenset' over 'set' gave me a saving in time or
memory (even if tiny) I would favour immutable sets, where appropriate.


Well, obviously the "premature optimization" speech just went in one ear
and out the other. "Saving in time or memory" is what optimization is
about. What did you think optimization means?

set and frozenset have different functionality (one is mutable, the other
is not) and use cases (you use one where you need to dynamically add and
remove items from a set, and the other where you need to use a set as a
key in a dictionary). In most cases, they aren't interchangable. While
you're spending time worrying about shaving a thousandth of a millisecond
off a program that takes five seconds to run, I'll get on with development
using the right object for the functionality needed.

--
Steven.

Jul 21 '05 #8
"Steven D'Aprano" <st***@REMOVETHIScyber.com.au> wrote:
On Wed, 06 Jul 2005 14:25:30 +0100, Will McGugan wrote:
No need for the 'premature optimization is the root of all evil' speech.
I'm not trying to optimize anything - just enquiring about the nature of
frozenset. If typing 'frozenset' over 'set' gave me a saving in time or
memory (even if tiny) I would favour immutable sets, where appropriate.


Well, obviously the "premature optimization" speech just went in one ear
and out the other. "Saving in time or memory" is what optimization is
about. What did you think optimization means?

set and frozenset have different functionality (one is mutable, the other
is not) and use cases (you use one where you need to dynamically add and
remove items from a set, and the other where you need to use a set as a
key in a dictionary). In most cases, they aren't interchangable.


Well, they *may* be interchangable under some conditions, and that was
the OP's point you apparently missed. If you don't need to dynamically
modify a set and don't add sets as keys in dictionaries, you currently
have a choice; both frozenset and set will work. So all other things
being equal, it makes sense to ask about the relative performance if
the only 'evil' it may introduce can be rectified in a single import.

George

Jul 21 '05 #9
On Wed, 06 Jul 2005 10:15:31 -0700, George Sakkis wrote:
Well, they *may* be interchangable under some conditions, and that was
the OP's point you apparently missed.


I didn't miss anything of the sort. That's why I spent 15 minutes actually
producing test cases to MEASURE if there was any detectable speed
differences between accessing set and frozenset instead of wasting time
worrying about "tiny" differences in performance that are almost certainly
lost in the noise in real code.

If, over a thousand runs of the program, you save a millisecond of time in
total, but it costs you two seconds to type the comment in the code
explaining why you used frozenset instead of the more natural set, then
your "optimization" is counter-productive. Even just considering the
question is a waste of valuable developer time! THAT is the lesson of
premature optimization: don't even THINK about optimizing code until you
have it WORKING and you have MEASURED that it is too slow.
--
Steven.
Jul 21 '05 #10
"Steven D'Aprano" <st***@REMOVETHIScyber.com.au> wrote:
On Wed, 06 Jul 2005 10:15:31 -0700, George Sakkis wrote:
Well, they *may* be interchangable under some conditions, and that was
the OP's point you apparently missed.


I didn't miss anything of the sort. That's why I spent 15 minutes actually
producing test cases to MEASURE if there was any detectable speed
differences between accessing set and frozenset instead of wasting time
worrying about "tiny" differences in performance that are almost certainly
lost in the noise in real code.

If, over a thousand runs of the program, you save a millisecond of time in
total, but it costs you two seconds to type the comment in the code
explaining why you used frozenset instead of the more natural set, then
your "optimization" is counter-productive. Even just considering the
question is a waste of valuable developer time! THAT is the lesson of
premature optimization: don't even THINK about optimizing code until you
have it WORKING and you have MEASURED that it is too slow.


I fully agree with your last sentence, first profile and then consider
if it's worth optimizing. Still, you either missed the point again or
you are arguing for the sake of the argument. Neither me nor the OP
claimed that it's worthwhile saving a millisecond or two. The OP hadn't
done the timing measurements you did, so he didn't know in advance if
the difference is 1 millisecond or 500. After you replied with evidence
that it's not worth it, he made clear that he was just enquiring, not
optimizing prematurely as you took for granted and ranted against it
from the beginning.

Hope it's clear now,

George

Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stefan Behnel | last post by:
Hi! frozenset() doesn't behave as the other immutable empty data types in 2.4: ..>>> '' is '' True ..>>> () is () True ..>>> frozenset() is frozenset() False
5
by: Jacob Page | last post by:
I have released interval-0.2.1 at http://members.cox.net/apoco/interval/. IntervalSet and FrozenIntervalSet objects are now (as far as I can tell) functionality equivalent to set and frozenset...
3
by: Mark E. Fenner | last post by:
Hello all, I was migrating some code from sets.ImmutableSet to frozenset and noticed the following: ******code******** #!/usr/bin/env python from sets import ImmutableSet
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
37
by: Gregory Guthrie | last post by:
I am comparing Python to a few other scripting languages, and used a simple anagrams program as a sample. I was surprised ast a few python features that did not work as I would expect/wish;...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
7
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
s0 = set() In that case, there is always a trivial answer. As I can use any function which takes and returns sets, and as I shall come up with a function that returns s0, I just use the...
3
by: srinivasan srinivas | last post by:
Hi, I am getting an error while executing the following snippet. If i comment out method __repr__ , it works fine. class fs(frozenset):     def __new__(cls, *data):         data = sorted(data)...
0
by: Terry Reedy | last post by:
srinivasan srinivas wrote: When posting problems like this, please include the Python version. If you ran this with 2.6, please run the following: x = frozenset('abc') y = frozenset('bcd') z...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.