Bob Arning arning@charm.net wrote:
Hi all,
Is the following a known and accepted feature of our sorting algorithm?
[((1 to: 10000) collect: [ :x | x]) asSortedCollection] timeToRun ==> 98
[((1 to: 10000) collect: [ :x | 3]) asSortedCollection] timeToRun ==> 20826
Cheers, Bob
No, I don't think it is known and accepted.
Another test: [((1 to: 10000) collect: [ :x | 10001 - x]) asSortedCollection] timeToRun ==> 87
asSortedCollection
calls
newFrom: aCollection "implemented in OrderedCollection" "Answer an instance of me containing the same elements as aCollection."
| newCollection | newCollection _ self new: aCollection size. newCollection addAll: aCollection. ^newCollection
And #addAll: is implemented in SortedCollection
addAll: aCollection aCollection size > (self size // 3) ifTrue: [aCollection do: [:each | self addLast: each]. self reSort] ifFalse: [aCollection do: [:each | self add: each]]. ^ aCollection
which calls #add:
add: newObject ^ super insert: newObject before: (self indexForInserting: newObject)
which finally calls #insert:before:
and #indexForInserting:
Two methods which are fairly complex and must contain an error somewhere.
Are there sorting specialists out there?
Regards Hannes
On Wed, 1 May 2002, Hannes Hirzel wrote:
Bob Arning arning@charm.net wrote:
Hi all,
Is the following a known and accepted feature of our sorting algorithm?
[((1 to: 10000) collect: [ :x | x]) asSortedCollection] timeToRun ==> 98
[((1 to: 10000) collect: [ :x | 3]) asSortedCollection] timeToRun ==> 20826
Are there sorting specialists out there?
Maybe....
But why not run a it under MessageTally and paste the results? That should almost immediately indicate what parts of the code are to blame.
Scott
Well, actually...
The default for #asSortedCollection is <=. If instead, one were to replace this with:
[((1 to: 10000) collect: [:x |3]) asSorterdCollection:[ :a :b | a<b ] timeToRun ==> 183
compared with:
[((1 to: 10000) collect: [:x |3]) asSorterdCollection:[ :a :b | a<=b]] timeToRun ==> 29150
Obviously, the comparison is always true, hence something always happens (probably a swap).
Hence, not a bug - though I think sorting on <= is inefficient. DAS
At 06:00 PM 5/1/2002 +0100, you wrote:
Bob Arning arning@charm.net wrote:
Hi all,
Is the following a known and accepted feature of our sorting algorithm?
[((1 to: 10000) collect: [ :x | x]) asSortedCollection] timeToRun ==> 98
[((1 to: 10000) collect: [ :x | 3]) asSortedCollection] timeToRun ==> 20826
Cheers, Bob
No, I don't think it is known and accepted.
Another test: [((1 to: 10000) collect: [ :x | 10001 - x]) asSortedCollection] timeToRun ==> 87
asSortedCollection
calls
newFrom: aCollection "implemented in OrderedCollection" "Answer an instance of me containing the same elements as aCollection."
| newCollection | newCollection _ self new: aCollection size. newCollection addAll: aCollection. ^newCollection
And #addAll: is implemented in SortedCollection
addAll: aCollection aCollection size > (self size // 3) ifTrue: [aCollection do: [:each | self addLast: each]. self reSort] ifFalse: [aCollection do: [:each | self add: each]]. ^ aCollection
which calls #add:
add: newObject ^ super insert: newObject before: (self indexForInserting: newObject)
which finally calls #insert:before:
and #indexForInserting:
Two methods which are fairly complex and must contain an error somewhere.
Are there sorting specialists out there?
Regards Hannes
On Wednesday 01 May 2002 09:59 am, David A. Smith wrote:
Well, actually...
The default for #asSortedCollection is <=. If instead, one were to replace this with:
[((1 to: 10000) collect: [:x |3]) asSorterdCollection:[ :a :b | a<b ] timeToRun ==> 183
compared with:
[((1 to: 10000) collect: [:x |3]) asSorterdCollection:[ :a :b | a<=b]] timeToRun ==> 29150
Obviously, the comparison is always true, hence something always happens (probably a swap).
Hence, not a bug - though I think sorting on <= is inefficient.
It is, but I think there is a bug.
The main problem seems to be that defaultSort:to: recurses way too much. I'd expect its recursion depth to max out at log2(size) (I think), but it's going down to about (size).
I ran it on 100 items, and it got to a recursion depth of 98 (!). Twice.
defaultSort: 1 to: 1000 calls defaultSort: 1 to: 0 and defaultSort: 2 to: 1000
which doesn't seem right. For one thing, sorting 1 to: 0 is a waste of time.
squeak-dev@lists.squeakfoundation.org