[Newbies] Re: how to sort by key a Dictionary object?

Bert Freudenberg bert at freudenbergs.de
Wed Aug 26 15:07:58 UTC 2009


On 26.08.2009, at 16:50, Randal L. Schwartz wrote:

> As we were discussing this issue last night at the Portland  
> Smalltalk meeting
> after-meeting dinner, whether it is faster to grab the associations  
> to sort
> them, or to grab the keys to sort them first then go look up the  
> values,
> depends on the storage of a Dictionary.  And I suspect that since  
> the Squeak
> implementation of Dictionary already has the associations and just  
> needs to
> spit them out, that's probably going to be faster than going back to  
> look
> them up one by one, as the above code does.


This is leaving newbie-territory, but don't speculate about  
performance, measure instead:

| dict |
dict := Dictionary new.
100000 timesRepeat: [dict at: 1000000 atRandom put: 'dummy'].
{
[dict keys asSortedCollection do: [:key | | value | value := dict at:  
key]] timeToRun.
[dict associations asSortedCollection do: [:assoc | | key value |  
key := assoc key. value := assoc value]] timeToRun.
}

... and this would give the advantage to using #keys.

- Bert -




More information about the Beginners mailing list