Dictionary>>collect: vs. Dictionary>>select: : why are theydifferent?

Andreas Raab andreas.raab at gmx.de
Sat Nov 15 01:15:36 UTC 2003


[Re: Dictionary>>collect:]
> Can anyone say whether this is the desired behavior?

Good question. A better one might even be if that's "reasonable" behavior.
If I look at #collect: it does appear to be more reasonable to answer a
dictionary which maps the same keys to different values.

> Also, it looks like I can gain about 20% in speed simply, and 
> also reduce the creation of extra objects (we don't *really* need
> Associations in the dictionary, do we?):

I would not want to change this. If you're planning to put away with
associations for certain kinds of dictionaries (for example,
MethodDictionary is an IDDict which has no associations) it seems better to
set up a new branch in the class hierarchy. Call it Map and IdentityMap if
you want to.

Cheers,
  - Andreas

> -----Original Message-----
> From: squeak-dev-bounces at lists.squeakfoundation.org 
> [mailto:squeak-dev-bounces at lists.squeakfoundation.org] On 
> Behalf Of Ned Konz
> Sent: Saturday, November 15, 2003 2:07 AM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Dictionary>>collect: vs. Dictionary>>select: : why 
> are theydifferent?
> 
> 
> I was just trying to get better performance on 
> IdentityDictionaries (which was 
> pretty easy to do, BTW), and in going through the Dictionary 
> protocol I ran 
> into something I'd never noticed before:
> 
> Dictionary>>select: returns a Dictionary
> 
> while
> 
> Dictionary>>collect: returns an OrderedCollection (!) of 
> result values.
> It does seem peculiar that this should be an 
> OrderedCollection, since the 
> associations are visited by hash order.
> 
> d _ IdentityDictionary new.
> 1 to: 10 do: [ :i | d at: i put: i * 2 ].
> d collect: [ :ea | ea ]
> 	an OrderedCollection(2 4 6 8 10 12 14 16 18 20)
> d select: [ :ea | true ]
> 	an IdentityDictionary(1->2 2->4 3->6 4->8 5->10 6->12 
> 7->14 8->16 9->18 
> 10->20 )
> d reject: [ :ea | false ]
> 	an IdentityDictionary(1->2 2->4 3->6 4->8 5->10 6->12 
> 7->14 8->16 9->18 
> 10->20 )
> 
> Can anyone say whether this is the desired behavior?
> 
> Also, it looks like I can gain about 20% in speed simply, and 
> also reduce the 
> creation of extra objects (we don't *really* need Associations in the 
> dictionary, do we?):
> 
> d1 _ IdentityDictionary new.
> d2 _ IdentityDictionary2 new.
> 1 to: 5 do: [ :i | d1 at: i put: i ].
> 1 to: 5 do: [ :i | d2 at: i put: i ].
> 
> Smalltalk garbageCollect.
> Time millisecondsToRun: [ 100000 timesRepeat: [ d1 at: 2 ]]   165
> Smalltalk garbageCollect.
> Time millisecondsToRun: [ 100000 timesRepeat: [ d2 at: 2 ]]   136
> Smalltalk garbageCollect.
> Time millisecondsToRun: [ 1 to: 100000 do: [ :i | d1 at: i 
> put: i ]]  852
> Smalltalk garbageCollect.
> Time millisecondsToRun: [ 1 to: 100000 do: [ :i | d2 at: i 
> put: i ]] 678
> 
> 
> -- 
> Ned Konz
> http://bike-nomad.com
> GPG key ID: BEEA7EFE
> 
> 




More information about the Squeak-dev mailing list