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*jvyyzptht na^pbz") 10 4049
On 7/6/05, Will McGugan <ne**@nowillmcg uganspam.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.spreadfiref ox.com/?q=affiliates&a mp;id=67907& ;t=1>
Qiangning Hong wrote: On 7/6/05, Will McGugan <ne**@nowillmcg uganspam.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*jvyyzptht na^pbz")
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.ImmutableS et(range(500000 ))
py> assert len(bigset) == len(bigimmutabl eset)
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.1153950691223 1445
py> tester(bigimmut ableset, range(100, 500))
0.1201419830322 2656
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.1099889278411 8652
py> tester(bigimmut ableset, range(100, 500))
0.1111409664154 0527
The difference is insignificant. How about unsuccessful checks?
py> tester(bigset, range(-100, -500, -1))
0.1207029819488 5254
py> tester(bigset, range(-100, -500, -1))
0.1168141365051 2695
py> tester(bigimmut ableset, range(-100, -500, -1))
0.1131389141082 7637
py> tester(bigimmut ableset, range(-100, -500, -1))
0.1131570339202 8809
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.
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*jvyyzptht na^pbz")
Will McGugan <ne**@NOwillmcg uganSPAM.com> writes: Qiangning Hong wrote: On 7/6/05, Will McGugan <ne**@nowillmcg uganspam.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
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
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.
"Steven D'Aprano" <st***@REMOVETH IScyber.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
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 "optimizati on" 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 objects, except they can contain
intervals as well as discrete values.
Though I have my own unit tests for verifying this claim, I'd like to
run my code through actual set and frozenset unit tests. Does any such
code exist? Is it in pure...
|
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
|
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 not work. It only works on lists and strings, for no obvious
reason. Why not on all sequence types?
Or, another example, the index() method has start and end parameters for
lists and strings. The count() method also has start and end parameters...
|
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; which caused less compact/expressive program styles that I
wanted - reverting to a FORTRAN like series of assignments.
For example,
- why is len() not a member function of strings? Instead one says len(w).
| |
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.
|
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 following function
def f(s):
return s0
To compute s0, just invoke f with any other of the sets, and you
|
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)
self = frozenset.__new__(cls, data)
self.__data = data
return self
|
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 = x.difference(y)
print(id(x) != id(z))
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |