'From Cuis 5.0 [latest update: #3956] on 11 November 2019 at 9:23:39 am'! !BlockClosure methodsFor: 'evaluating' stamp: 'sqr - jmv 11/11/2019 09:21:56'! 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 value description | "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 ]. value _ count * 1000 / totalMilliseconds. description _ ' runs per second'. value < 1 ifTrue: [ value _ 1.0 / value. description _ ' seconds per run' ]. ^ value withDecimalUnitPrefixAndValue: [ :valueForUnit :unitPrefixSymbol :unitPrefixName | String streamContents: [ :strm | valueForUnit printOn: strm fractionDigits: 2. strm space; nextPutAll: unitPrefixSymbol; nextPutAll: description ]]! ! !Number methodsFor: 'printing' stamp: 'jmv 11/11/2019 09:15:50'! withBinaryUnitPrefixAndValue: aBlock " As in https://en.wikipedia.org/wiki/Binary_prefix { 0.123456. 1.23456. 12.3456. 123.456. 1234.56. 12345.6. 123456. 1234560. 12345600 } do: [ :n | n withBinaryUnitPrefixAndValue: [ :value :unitPrefixSymbol :unitPrefixName | {value printString, ' ', unitPrefixSymbol. unitPrefixName} print]] " | prefixIndex factor nameAndSymbol | prefixIndex _ self log floor // 3. prefixIndex _ prefixIndex min: 8 max: 0. factor _ 1024 raisedToInteger: prefixIndex. nameAndSymbol _ { {''. ''}. {'kibi'. 'Ki'}. {'mebi'. 'Mi'}. {'gibi'. 'Gi'}. {'tebi'. 'Ti'}. {'pebi'. 'Pi'}. {'exbi'. 'Ei'}. {'zebi'. 'Zi'}. {'yobi'. 'Yi'} } at: prefixIndex+1. ^aBlock value: (self / factor) asIntegerOrFloat value: nameAndSymbol second value: nameAndSymbol first! ! !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: 'jmv 11/11/2019 09:16:09'! printStringAsBytes "Answer a terse, easily-readable representation of this Integer reprsenting a number of bytes. Useful for file-browsers. 123 printStringAsBytes 1024 printStringAsBytes (12*1024) printStringAsBytes (1024*1024) printStringAsBytes (1024*1024*1024) printStringAsBytes (1024*1024*1024*1024) printStringAsBytes (30 factorial) printStringAsBytes See https://en.wikipedia.org/wiki/Kibibyte See #printStringAsBytesDecimal " ^self withBinaryUnitPrefixAndValue: [ :value :unitPrefixSymbol :unitPrefixName | String streamContents: [ :strm | value printOn: strm fractionDigits: 2. strm space; nextPutAll: unitPrefixSymbol; nextPut: $B]]! ! !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! !