The Computer Language
24.04 Benchmarks Game

spectral-norm Pharo Smalltalk program

source code

"* The Computer Language Benchmarks Game
    https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
    contributed by Paolo Bonzini 
    reworked by Isaac Gouy *"!

Object subclass: #BenchmarksGame
   instanceVariableNames: ''
   classVariableNames: ''
   poolDictionaries: ''
   category: ''!


!BenchmarksGame class methodsFor: 'private'!

spectralnorm: n
   | u v vBv vv |
   u := Array new: n withAll: 1.0d0.
   10 timesRepeat:
      [v := u multiplyAtAv.
       u := v multiplyAtAv].
   vBv := 0.0d0.
   vv := 0.0d0.
   1 to: n do:
      [:i |
       vBv := vBv + ((u at: i) * (v at: i)).
       vv := vv + ((v at: i) * (v at: i))].
   ^(vBv / vv) sqrt! !

!BenchmarksGame class methodsFor: 'initialize-release'!

do: n
   Stdio stdout
      print: (self spectralnorm: n) digits: 9;
      nl! !


!SmallInteger methodsFor: 'benchmarks game'!

matrixA: anInteger
   ^1.0d0 / ((self + anInteger - 2) * (self + anInteger - 1) /2  + self)! !


!Array methodsFor: 'benchmarks game'!

multiplyAtv
   | n atv sum |
   n := self size.
   atv := Array new: n.
   1 to: n do: [:i|
      sum := 0.0d0.
      1 to: n do: [:j|
         sum := sum + ((j matrixA: i) * (self at: j)) ].
      atv at: i put: sum].
   ^atv!

multiplyAtAv
   ^(self multiplyAv) multiplyAtv!

multiplyAv
   | n av sum |
   n := self size.
   av := Array new: n.
   1 to: n do: [:i|
      sum := 0.0d0.
      1 to: n do: [:j|
         sum := sum + ((i matrixA: j) * (self at: j)) ].
      av at: i put: sum].
   ^av! !


!StdioStream methodsFor: 'benchmarks game'!

nl
   self nextPut: Character lf!

print: number digits: decimalPlaces
   | precision rounded |
   decimalPlaces <= 0 ifTrue: [^ number rounded printString].
   precision := (10 raisedTo: decimalPlaces negated) asFloat.
   rounded := number roundTo: precision.
   self nextPutAll: 
      ((rounded asScaledDecimal: decimalPlaces) printString copyUpTo: $s)! !


    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Pharo 10.1.1
Mar 12 2024 13:57:46
Compiler: 5.4.0 20160609


 Fri, 15 Mar 2024 22:44:48 GMT

MAKE:
cp /opt/src/pharo-vm-Linux-x86_64-stable/Pharo11-SNAPSHOT-64bit-aece1b5.image spectralnorm.pharo_run.image
cp /opt/src/pharo-vm-Linux-x86_64-stable/Pharo11-SNAPSHOT-64bit-aece1b5.changes spectralnorm.pharo_run.changes
ln -s /opt/src/pharo-vm-Linux-x86_64-stable/Pharo11.0-64bit-aece1b5.sources .
cat Include/pharo/make.st
| prog |

(SystemWindow windowsIn: World
      satisfying: [:w | w model canDiscardEdits])
   do: [:w | w delete].

   "load program to be measured"
prog := Smalltalk getSystemAttribute: 3.
(prog notNil) ifTrue: [prog asFileReference fileIn].

ImageCleaner cleanUpForRelease.
Smalltalk garbageCollect.
SmalltalkImage current snapshot: true andQuit: true.
/opt/src/pharo-vm-Linux-x86_64-stable/pharo --headless spectralnorm.pharo_run.image Include/pharo/make.st spectralnorm.pharo
cat Include/pharo/main.st

BenchmarksGame do: (Smalltalk getSystemAttribute: 3) asInteger.!
SmalltalkImage current snapshot: false andQuit: true!



27.85s to complete and log all make actions

COMMAND LINE:
 /opt/src/pharo-vm-Linux-x86_64-stable/pharo --headless spectralnorm.pharo_run.image Include/pharo/main.st 5500

PROGRAM OUTPUT:
1.274224153