#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Sun, 06 Nov 2016 12:09:07 +0100
changeset 20861 9ef9874be1b1
parent 20860 216e672e8d44
child 20862 b0954ce02996
#FEATURE by cg class: Block changed: #benchmark: also show cpu cycles.
Block.st
--- a/Block.st	Sat Nov 05 19:13:40 2016 +0100
+++ b/Block.st	Sun Nov 06 12:09:07 2016 +0100
@@ -877,24 +877,35 @@
 benchmark:anInfoString
     "evaluate myseld and show the timing info on Transcript"
 
-    |startTime endTime overhead micros millis|
+    |startTime endTime startCycles endCycles overhead overheadCycles 
+     micros millis cycles|
 
     startTime := OperatingSystem getMicrosecondTime.
+    startCycles := OperatingSystem getCPUCycleCount.
     [123] value.
+    endCycles := OperatingSystem getCPUCycleCount.
     endTime := OperatingSystem getMicrosecondTime.
     overhead := endTime - startTime.
+    "/ just in case, the OS does not support cpu cycles
+    startCycles notNil ifTrue:[ overheadCycles := endCycles - startCycles ].
     
     startTime := OperatingSystem getMicrosecondTime.
+    startCycles := OperatingSystem getCPUCycleCount.
     self value.
+    endCycles := OperatingSystem getCPUCycleCount.
     endTime := OperatingSystem getMicrosecondTime.
 
     micros := (endTime - startTime - overhead) max:0.
-
+    "/ just in case, the OS does not support cpu cycles
+    startCycles notNil ifTrue:[
+        cycles := (endCycles - startCycles - overheadCycles) max:0. 
+    ].
+    
     Transcript show:anInfoString.
     micros < 1000 ifTrue:[
         "/ too stupid: many fonts do not have a mu,
         "/ so I output it as us here.
-        Transcript show:micros; show:' us'.
+        Transcript show:micros; show:' µs'.
     ] ifFalse:[
         micros < 100000 ifTrue:[
             millis := (micros / 1000.0) asFixedPointRoundedToScale:2.
@@ -904,11 +915,23 @@
             Transcript show:(TimeDuration milliseconds:millis).
         ].
     ].
+    cycles notNil ifTrue:[
+        Transcript show:' ('; show:cycles; show:' cycles)'.
+    ].
     Transcript cr.
 
     "
-        [10 factorial] benchmark:'10 factorial:'
-        [100 factorial] benchmark:'100 factorial:'
+     be aware that if you evaluate the following,
+     the blocks will be interpreted by the doIt. 
+     Thus you will get on-realistic values.
+     Better compile those expressions into a method and call that
+     for realistic measurements.
+     
+     [] benchmark:'empty block:'        - this is a pre-compiled block
+     [123] benchmark:'empty block:'     - the rest are interpreted blocks
+     [10 factorial] benchmark:'10 factorial:'
+     [10 factorial] benchmark:'11 factorial:'
+     [100 factorial] benchmark:'100 factorial:'
     "
 ! !