[FIX] SortedCollectionFix-sr

Stephan Rudlof sr at evolgo.de
Fri Sep 27 16:29:13 UTC 2002


Andreas,

after my correction in
  correction [was: Re: [FIX] SortedCollectionFix-sr]
, I think the reason for the performance loss in the 'unoptimized' version
must be the temporarily construction of a *Set* of keys  (an
OrderedCollection would be cheaper) in:

Dictionary>>
keysSortedSafelyAR
	"Answer a SortedCollection containing the receiver's keys."
	^ SortedCollection
		withAll: self keys
		sortBlock:
			[:x :y |  "Should really be use <obj, string, num> compareSafely..."
			((x isString and: [y isString])
				or: [x isNumber and: [y isNumber]])
				ifTrue: [x < y]
				ifFalse: [x class == y class
					ifTrue: [x printString < y printString]
					ifFalse: [x class name < y class name]]]

compared with the older one, where is an iteration directly over the keys
(>>addLastWithoutSorting: just calls "super addLast:"):

Dictionary
>>keysSortedSafely
	"Answer a SortedCollection containing the receiver's keys."
	| sortedKeys |
	sortedKeys _ SortedCollection new: self size.
	sortedKeys sortBlock:
		[:x :y |  "Should really be use <obj, string, num> compareSafely..."
		((x isString and: [y isString])
			or: [x isNumber and: [y isNumber]])
			ifTrue: [x < y]
			ifFalse: [x class == y class
				ifTrue: [x printString < y printString]
				ifFalse: [x class name < y class name]]].
	self keysDo: [:each | sortedKeys addLastWithoutSorting: each].
	^ sortedKeys reSort

Greetings,

Stephan

Stephan Rudlof wrote:
> Andreas,
> 
> I have made some tests (>>keysSortedSafelyAR is according your suggestion):
> 
> | t1 t2 dicts |
> Smalltalk garbageCollect.
> dicts _ Dictionary allInstances.
> Smalltalk garbageCollect.
> t1 _ [10 timesRepeat: [dicts do: [:dict | dict keysSortedSafelyAR]]] timeToRun.
> Smalltalk garbageCollect.
> t2 _ [10 timesRepeat: [dicts do: [:dict | dict keysSortedSafely]]] timeToRun.
> {t1.t2}
>  #(25972 22073) #(25942 22996) #(25575 22139)
> 
> | t1 t2 bigDict |
> Smalltalk garbageCollect.
> bigDict _ (Dictionary allInstances asSortedCollection: [:d1 :d2 | d1 size >
> d2 size]) first.
> Smalltalk garbageCollect.
> t1 _ [10 timesRepeat: [bigDict keysSortedSafelyAR]] timeToRun.
> Smalltalk garbageCollect.
> t2 _ [10 timesRepeat: [bigDict keysSortedSafely]] timeToRun.
> {t1.t2. bigDict size}
>  #(1716 1297 952) #(1718 1297 952) #(1742 1298 952)


<...>




More information about the Squeak-dev mailing list