Levente Uzonyi uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-ul.904.mcz ==================== Summary ==================== Name: Kernel-ul.904 Author: ul Time: 17 February 2015, 1:40:49.811 pm UUID: 7e873979-7e22-4abb-bd69-fa6d72f8c7fc Ancestors: Kernel-nice.902 Avoid the frequent time checks, and the clock rollover bug in BlockClosure >> #bench. Introduced BlockClosure >> #benchFor:, a variant of #bench, which takes the duration of the benchmark as its parameter. Added more information about the running times to the answer. E.g.: [ 10 factorial ] benchFor: 0.1 seconds. "'7,990,000 per second. 125 nanoseconds per run.'" [ 100 factorial ] benchFor: 10 seconds. "'19,900 per second. 50.3 microseconds per run.'" [ 1000 factorial ] bench. "'378 per second. 2.65 milliseconds per run.'" [ 20000 factorial ] bench. "'0.843 per second. 1.19 seconds per run.'" =============== Diff against Kernel-nice.902 =============== Item was changed: ----- Method: BlockClosure>>bench (in category 'evaluating') ----- bench "See how many times I can value in 5 seconds. I'll answer a meaningful description." + ^self benchFor: 5 seconds! - | startTime endTime count roundTo3Digits | - roundTo3Digits := [:num | - | rounded lowDigit | - rounded := (num * 1000) rounded. "round to 1/1000" - lowDigit := (rounded numberOfDigitsInBase: 10) - 3. "keep only first 3 digits" - rounded := rounded roundTo:(10 raisedTo: lowDigit). - (lowDigit >= 3 or: [rounded \\ 1000 = 0]) "display fractional part only when needed" - ifTrue: [(rounded // 1000) asStringWithCommas] - ifFalse: [(rounded / 1000.0) printString]]. - count := 0. - endTime := Time millisecondClockValue + 5000. - startTime := Time millisecondClockValue. - [ Time millisecondClockValue > endTime ] whileFalse: [ self value. count := count + 1 ]. - endTime := Time millisecondClockValue. - ^count = 1 - ifTrue: [ (roundTo3Digits value: (endTime - startTime) / 1000) , ' seconds.' ] - ifFalse: - [ (roundTo3Digits value: (count * 1000) / (endTime - startTime)) , ' per second.' ]! Item was added: + ----- Method: BlockClosure>>benchFor: (in category 'evaluating') ----- + benchFor: aDuration + "See how many times I can value within the given duration. I'll answer a meaningful description." + + | startTime shouldRun count elapsedTime roundTo3Digits delay | + roundTo3Digits := [:num | + | rounded lowDigit | + rounded := (num * 1000) rounded. "round to 1/1000" + lowDigit := (rounded numberOfDigitsInBase: 10) - 3. "keep only first 3 digits" + rounded := rounded roundTo:(10 raisedTo: lowDigit). + (lowDigit >= 3 or: [rounded \\ 1000 = 0]) "display fractional part only when needed" + ifTrue: [(rounded // 1000) asStringWithCommas] + ifFalse: [(rounded / 1000.0) printString]]. + delay := aDuration asDelay. + count := 0. + shouldRun := true. + [ delay wait. shouldRun := false ] forkAt: Processor timingPriority - 1. + startTime := Time millisecondClockValue. + [ shouldRun ] whileTrue: [ + self value. + count := count + 1 ]. + elapsedTime := Time millisecondsSince: startTime. + ^(roundTo3Digits value: count * 1000 / elapsedTime) , ' per second.', (( + #( + (1e-3 'seconds') + (1 'milliseconds') + (1e3 'microseconds') + (1e6 'nanoseconds') + ) + detect: [ :pair | elapsedTime * pair first >= count ] + ifNone: [ #(1e9 'picoseconds') ]) + in: [ :pair | + ' {1} {2} per run.' format: { + (roundTo3Digits value: elapsedTime * pair first / count). + pair second } ])! |
Free forum by Nabble | Edit this page |