'From Cuis 5.0 [latest update: #3944] on 10 November 2019 at 6:34:12 pm'! !Number methodsFor: 'printing' stamp: 'sqr 11/10/2019 18:24:34'! withDecimalUnitPrefixAndValue: aBlock " As in https://en.wikipedia.org/wiki/Metric_prefix { 0.00000123456. 0.0000123456. 0.000123456. 0.00123456. 0.0123456. 0.123456. 1.23456. 12.3456. 123.456. 1234.56. 12345.6. 123456. 1234560. 12345600 } do: [ :n | n withDecimalUnitPrefixAndValue: [ :value :unitPrefixSymbol :unitPrefixName | {value printString, ' ', unitPrefixSymbol. unitPrefixName} print]] " | prefixIndex factor nameAndSymbol | prefixIndex _ self log floor // 3. prefixIndex _ prefixIndex min: 6 max: -6. factor _ 1000 raisedToInteger: prefixIndex. nameAndSymbol _ { {'atto'. 'a'}. {'femto'. 'f'}. {'pico'. 'p'}. {'nano'. 'n'}. {'micro'. 'µ'}. {'milli'. 'm'}. {''. ''}. {'kilo'. 'k'}. {'mega'. 'M'}. {'giga'. 'G'}. {'tera'. 'T'}. {'peta'. 'P'}. {'exa'. 'E'} } at: prefixIndex+7. ^aBlock value: self asFloat / factor value: nameAndSymbol second value: nameAndSymbol first! ! !Integer methodsFor: 'printing' stamp: 'sqr 11/10/2019 18:24:47'! printStringAsBytesDecimal "Answer a terse, easily-readable representation of this Integer reprsenting a number of bytes. Useful for file-browsers. 123 printStringAsBytesDecimal (12*1000) printStringAsBytesDecimal (1000*1000) printStringAsBytesDecimal 1024 printStringAsBytesDecimal (12*1024) printStringAsBytesDecimal (1024*1024) printStringAsBytesDecimal (1024*1024*1024) printStringAsBytesDecimal (1024*1024*1024*1024) printStringAsBytesDecimal (30 factorial) printStringAsBytesDecimal See https://en.wikipedia.org/wiki/Kibibyte See #printStringAsBytes " ^self withDecimalUnitPrefixAndValue: [ :value :unitPrefixSymbol :unitPrefixName | String streamContents: [ :strm | value printOn: strm fractionDigits: 2. strm space; nextPutAll: unitPrefixSymbol; nextPut: $B]]! ! !BlockClosure methodsFor: 'evaluating' stamp: 'sqr 11/10/2019 10:17:21'! bench "See how many times I can value in 5 seconds. I'll answer a meaningful description. [ Float pi printString ] bench. [ 80000 factorial printString ] bench. " ^self benchFor: 5 seconds! ! !BlockClosure methodsFor: 'evaluating' stamp: 'sqr 11/10/2019 18:31:15'! benchFor: aDuration "See how many times I can value in 5 seconds. I'll answer a meaningful description. [ Float pi printString ] benchFor: 100 milliSeconds. [ 80000 factorial printString ] benchFor: 1 seconds. [ 2 + 3 ] benchFor: 1.5 seconds. [ 2 + 3 ] benchFor: 1.6 seconds. [ 2 + 3. 2 + 3 ] benchFor: 1.6 seconds. [] benchFor: 2 seconds. " | totalMilliseconds startTime count run | "Same initial conditions" Smalltalk garbageCollect. "Side effect: the measurement code starts jitted" count _ 1. totalMilliseconds _ self timeToRun. totalMilliseconds * 32 < aDuration totalMilliseconds ifTrue: [ count _ 0. run _ true. [ (Delay forDuration: aDuration) wait. run _ false ] forkAt: Processor timingPriority - 1. startTime _ Time localMillisecondClock. [ run ] whileTrue: [ self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. self value. count _ count + 32 ]. totalMilliseconds _ Time localMillisecondClock - startTime ]. ^count * 1000 / totalMilliseconds withDecimalUnitPrefixAndValue: [ :value :unitPrefixSymbol :unitPrefixName | String streamContents: [ :strm | value printOn: strm fractionDigits: 2. strm space; nextPutAll: unitPrefixSymbol; nextPutAll: ' runs per second']]! !