The Computer Language
24.04 Benchmarks Game

binary-trees VW Smalltalk program

source code

"* The Computer Language Benchmarks Game
    https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
    contributed by Isaac Gouy
    modified by Eliot Miranda 
*"!


Smalltalk.Core defineClass: #BenchmarksGame
	superclass: #{Core.Object}
	indexedType: #none
	private: false
	instanceVariableNames: ''
	classInstanceVariableNames: ''
	imports: ''
	category: ''!

Smalltalk defineClass: #TreeNode
	superclass: #{Core.Object}
	indexedType: #none
	private: false
	instanceVariableNames: 'left right '
	classInstanceVariableNames: ''
	imports: ''
	category: 'benchmarks game'!

!Core.BenchmarksGame class methodsFor: 'initialize-release'!

do: n
   | minDepth maxDepth stretchDepth check longLivedTree iterations |
   minDepth := 4.
   maxDepth := minDepth + 2 max: n.
   stretchDepth := maxDepth + 1.

   check := (TreeNode bottomUpTree: stretchDepth) itemCheck.
   Stdout
      nextPutAll: 'stretch tree of depth '; print: stretchDepth; tab;
      nextPutAll: ' check: '; print: check; nl.

   longLivedTree := TreeNode bottomUpTree: maxDepth.
   minDepth to: maxDepth by: 2 do: [:depth|
      iterations := 1 bitShift: maxDepth - depth + minDepth.

      check := 0.
      1 to: iterations do: [:i|
         check := check + (TreeNode bottomUpTree: depth) itemCheck
         ].
      Stdout
         print: iterations; tab;
         nextPutAll: ' trees of depth '; print: depth; tab;
         nextPutAll: ' check: '; print: check; nl
      ].

   Stdout
      nextPutAll: 'long lived tree of depth '; print: maxDepth; tab;
      nextPutAll: ' check: '; print: longLivedTree itemCheck; nl.

   ^''! !


!TreeNode class methodsFor: 'instance creation'!

bottomUpTree: anInteger
   ^(anInteger > 0) 
      ifTrue: [
         self 
            left: (self bottomUpTree: anInteger - 1) 
            right: (self bottomUpTree: anInteger - 1)  
         ]
      ifFalse: [self left: nil right: nil]!

left: leftChild right: rightChild      
   ^(super new) left: leftChild right: rightChild! !


!TreeNode methodsFor: 'accessing'!

itemCheck
   ^left isNil 
      ifTrue: [1] ifFalse: [1 + left itemCheck + right itemCheck]! !

!TreeNode methodsFor: 'initialize-release'!

left: leftChild right: rightChild
   left := leftChild.
   right := rightChild! !


!Core.Stream methodsFor: 'benchmarks game'!

nl
   self nextPut: Character lf! !
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
VisualWorks® 8.3
Aug 19 2017


 Tue, 05 Mar 2024 06:39:56 GMT

MAKE:
cp /opt/src/vw8.3pul/image/visualnc64.im binarytrees.vw_run.im
/opt/src/vw8.3pul/bin/visual binarytrees.vw_run.im -nogui -pcl MatriX -filein binarytrees.vw -doit 'ObjectMemory snapshotThenQuit'

Autoloading MatriX from $(VISUALWORKS)/preview/matrix/MatriX.pcl
Autoloading Xtreams-Support from $(VISUALWORKS)/xtreams/Xtreams-Support.pcl
Autoloading Xtreams-Core from $(VISUALWORKS)/xtreams/Xtreams-Core.pcl
Autoloading Xtreams-Terminals from $(VISUALWORKS)/xtreams/Xtreams-Terminals.pcl
Autoloading Xtreams-Transforms from $(VISUALWORKS)/xtreams/Xtreams-Transforms.pcl
Autoloading Xtreams-Substreams from $(VISUALWORKS)/xtreams/Xtreams-Substreams.pcl
Autoloading Xtreams-Multiplexing from $(VISUALWORKS)/xtreams/Xtreams-Multiplexing.pcl
Filing in from:
	binarytrees.vw
BenchmarksGame class<initialize-release
TreeNode class<instance creation
TreeNode<accessing
TreeNode<initialize-release
Stream<benchmarks game
Do you want to add Root.Smalltalk.Core.Stream>>nl to the previously unchanged package, Collections-Streams
						OK to continue?
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/binarytrees.vw_run.im created at March 4, 2024 10:39:53 PM
7.78s to complete and log all make actions

COMMAND LINE:
 /opt/src/vw8.3pul/bin/visual binarytrees.vw_run.im -nogui -evaluate "BenchmarksGame do: 21"

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303