Dictionary>>collect: vs. Dictionary>>select: : why are they different?

Ned Konz ned at bike-nomad.com
Sat Nov 15 01:06:55 UTC 2003


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