The Computer Language
24.11 Benchmarks Game

binary-trees VW Smalltalk #3 program

source code

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

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: 'benchmarks game'!

do: n
   | checks depths iterations longLivedTree maxDepth minDepth
     nprocs stretchDepth |

   minDepth := 4.
   maxDepth := minDepth + 2 max: n.
   stretchDepth := maxDepth + 1.

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

   longLivedTree := TreeNode bottomUpTree: maxDepth.

   depths := minDepth to: maxDepth by: 2.
   iterations := depths collect: [:each| 1 bitShift: maxDepth - each + minDepth].

      "for larger workloads split the work across multiple processes"
   nprocs := (ExternalProcess shOne: 'nproc') asNumber.   
   (nprocs > 1 and: [n > 16]) 
      ifTrue: [
         | workers |        
         workers := MatriX.VirtualMachines new: nprocs.
         [checks := workers do: self checkBlock with: depths with: iterations]
            ensure: [workers release].
      ]
      ifFalse: [
         checks := OrderedCollection new.
         depths keysDo: [:j| checks add: 
            (self checkBlock value: (depths at: j) value: (iterations at: j))].
      ].        

   checks keysDo: [:i|
      Stdout
         print: (iterations at: i); tab;
         nextPutAll: ' trees of depth '; print: (depths at: i); tab;
         nextPutAll: ' check: '; print: (checks at: i); nl
   ].

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


checkBlock
   ^[:d :m| 
      | check |
      check := 0.
      1 to: m do: [:i| check := check + (TreeNode bottomUpTree: d) itemCheck].
      check
   ]! !


!TreeNode class methodsFor: 'instance creation'!

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

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


!TreeNode methodsFor: 'benchmarks game'!

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

!TreeNode methodsFor: 'instance creation'!

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


 Wed, 29 May 2024 00:21:52 GMT

MAKE:
cp /opt/src/vw8.3pul/image/visualnc64.im binarytrees.vw-3.vw_run.im
/opt/src/vw8.3pul/bin/visual binarytrees.vw-3.vw_run.im -nogui -pcl MatriX -filein binarytrees.vw-3.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-3.vw
BenchmarksGame class<benchmarks game
TreeNode class<instance creation
TreeNode<benchmarks game
TreeNode<instance creation
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-3.vw_run.im created at May 28, 2024 5:21:49 PM
7.85s to complete and log all make actions

COMMAND LINE:
 /opt/src/vw8.3pul/bin/visual binarytrees.vw-3.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