Application create: #DgScratchHome with: (#( Kernel) collect: [:each | Smalltalk at: each ifAbsent: [ Application errorPrerequisite: #DgScratchHome missing: each]])! DgScratchHome becomeDefault! Object subclass: #SimpleHanoi instanceVariableNames: '' classVariableNames: '' poolDictionaries: ''! DgScratchHome becomeDefault! Object subclass: #SpeedTester instanceVariableNames: '' classVariableNames: '' poolDictionaries: ''! DgScratchHome becomeDefault! Application subclass: #DgScratchHome instanceVariableNames: '' classVariableNames: '' poolDictionaries: ''! DgScratchHome becomeDefault! !SimpleHanoi class publicMethods ! test Transcript cr ; nextPutAll: 'Hanoi ', self new hanoi printString; cr! ! !SimpleHanoi publicMethods ! hanoi "SimpleHanoi new hanoi" ^(Time millisecondsToRun: [self move: 22 from: 1 to: 2 temp: 3]) / 1000.0! move: numberOfDisks from: source to: dest temp: temp numberOfDisks == 1 ifTrue: [^self]. self move: numberOfDisks-1 from: source to: temp temp: dest; move: numberOfDisks-1 from: temp to: dest temp: source! ! !SpeedTester class publicMethods ! run | results tester | results := Array new: 9. tester := self new. results at: 1 put: tester allocSpeedTest; at: 2 put: tester arrayWriteSpeedTest; at: 3 put: tester dictionaryWriteSpeedTest; at: 4 put: tester floatMathSpeedTest; at: 5 put: tester integerMathSpeedTest; at: 6 put: tester orderedCollectionIterateSpeedTest; at: 7 put: tester orderedCollectionWriteSpeedTest; at: 8 put: tester stringCompareSpeedTest; at: 9 put: (SimpleHanoi new hanoi). ^results! runThreeTimes " SpeedTester runThreeTimes " | results one two three | System globalGarbageCollect. one := self run. System globalGarbageCollect. two := self run. System globalGarbageCollect. three := self run. results := Array new: (one size). 1 to: results size do: [ :each | results at: each put: (((one at: each) + (two at: each) + (three at: each)) / 3.0) ]. ^results ! test Transcript cr ; nextPutAll: 'Allocate ', self new allocSpeedTest printString; cr; nextPutAll: 'Array write ', self new arrayWriteSpeedTest printString; cr; nextPutAll: 'Dictionary write ', self new dictionaryWriteSpeedTest printString; cr; nextPutAll: 'Float math ', self new floatMathSpeedTest printString; cr; nextPutAll: 'Integer math ', self new integerMathSpeedTest printString; cr; nextPutAll: 'OrderedCollection iterate ', self new orderedCollectionIterateSpeedTest printString; cr; nextPutAll: 'OrderedCollection write ', self new orderedCollectionWriteSpeedTest printString; cr; nextPutAll: 'String compare ', self new stringCompareSpeedTest printString; cr ! ! !SpeedTester publicMethods ! allocSpeedTest "SpeedTester new allocSpeedTest" System globalGarbageCollect. ^(Time millisecondsToRun: [ 100000 timesRepeat: [ Array new:10; new:10; new:10; new:10; new:10; new:10; new:10; new:10; new:10; new:10]. System globalGarbageCollect. ])/1000.0 ! arrayWriteSpeedTest | array junk | "SpeedTester new arrayWriteSpeedTest" junk := Object new. array := Array new: 10. ^(Time millisecondsToRun: [ 1000000 timesRepeat: [ array at: 1 put: junk; at: 2 put: junk; at: 3 put: junk; at: 4 put: junk; at: 5 put: junk; at: 6 put: junk; at: 7 put: junk; at: 8 put: junk; at: 9 put: junk; at: 10 put: junk]])/1000.0! dictionaryWriteSpeedTest | dict junk key1 key2 key3 key4 key5 key6 key7 key8 key9 key10 | "SpeedTester new dictionaryWriteSpeedTest" junk := Object new. key1 := Object new. key2 := Object new. key3 := Object new. key4 := Object new. key5 := Object new. key6 := Object new. key7 := Object new. key8 := Object new. key9 := Object new. key10 := Object new. ^(Time millisecondsToRun: [ 10000 timesRepeat: [ dict := IdentityDictionary new: 20. dict at: key1 put: junk; at: key2 put: junk; at: key3 put: junk; at: key4 put: junk; at: key5 put: junk; at: key6 put: junk; at: key7 put: junk; at: key8 put: junk; at: key9 put: junk; at: key10 put: junk]])/1000.0! floatMathSpeedTest "SpeedTester new floatMathSpeedTest" | a b c d e | a := 87.0. b := 53.0. c := -87.0. d := 42461.0. e := 5.0. ^(Time millisecondsToRun: [ 300000 timesRepeat: [ e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d]])/1000.0! integerMathSpeedTest "SpeedTester new integerMathSpeedTest" | a b c d e | a := 87. b := 53. c := -87. d := 42461. e := 5. ^(Time millisecondsToRun: [ 300000 timesRepeat: [ e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d. e := (e * a + b) * c + d]])/1000.0! orderedCollectionIterateSpeedTest | oc junk | "SpeedTester new orderedCollectionIterateSpeedTest" junk := Object new. oc := OrderedCollection new: 20. oc add: junk; add: junk; add: junk; add: junk; add: junk; add: junk; add: junk; add: junk; add: junk; add: junk. ^(Time millisecondsToRun: [ 100000 timesRepeat: [ oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]. oc do: [:element | ]]])/1000.0! orderedCollectionWriteSpeedTest | oc junk | "SpeedTester new orderedCollectionWriteSpeedTest" junk := Object new. ^(Time millisecondsToRun: [ 100000 timesRepeat: [ oc := OrderedCollection new: 20. oc addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk; addLast: junk]])/1000.0! stringCompareSpeedTest "SpeedTester new stringCompareSpeedTest" ^(Time millisecondsToRun: [ 100000 timesRepeat: [ 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings'. 'this is a test of a string compare of two long strings' = 'this is a test of a string compare of two long strings']])/1000.0! ! SimpleHanoi initializeAfterLoad! SpeedTester initializeAfterLoad! DgScratchHome initializeAfterLoad! DgScratchHome loaded!