[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 100000.
1 to: 10000 do: [ :i | c add: i ] ] ] timeToRun 3624
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 10000.
1 to: 10000 do: [ :i | c add: i ] ] ] timeToRun 2540
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 0.
1 to: 10000 do: [ :i | c add: i ] ] ] timeToRun 2879
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 1000.
1 to: 10000 do: [ :i | c add: i ] ] ] timeToRun 2784
Apparently Squeak takes more time to allocate the large array than Pharo? That would explain why the first run is so much slower than the other 3 - the exact opposite of Max's results (for Pharo 1.3 - but inline with Pharo4 - although not as extreme as Pharo4).
Then, what about an array that only adds at the beginning (instead of end)? I would expect worse performance - especially if I removed the space at the beginning. WIthout any of those changes, though:
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 100000.
1 to: 10000 do: [ :i | c addFirst: i ] ] ] timeToRun 2782
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 10000.
1 to: 10000 do: [ :i | c addFirst: i ] ] ] timeToRun 1845
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 0.
1 to: 10000 do: [ :i | c addFirst: i ] ] ] timeToRun 2052
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 1000.
1 to: 10000 do: [ :i | c addFirst: i ] ] ] timeToRun 1962
Weird that it turned out faster. Maybe it has to do with the first time it runs out of room, it move data (and doesn't allocate a new array?).
Stephan's code (in the same image):
[ 10000 timesRepeat: [ | c | c := OrderedCollection new: 100000.
c add: 1.
2 to: 10000 do: [ :i | c add: i beforeIndex: (i atRandom) ] ] ] timeToRun 200434
This last is much slower - every time it adds an value, it forces a copy of the data. It will not be as fast as the others.
-cbc