Block.st
branchjv
changeset 21024 8734987eb5c7
parent 20727 fb8c5591428b
parent 20870 fae257fc5172
child 21242 19fabe339f8b
--- a/Block.st	Wed Oct 26 23:35:39 2016 +0100
+++ b/Block.st	Fri Nov 18 20:48:04 2016 +0000
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
@@ -708,6 +710,8 @@
     "Created: / 28-08-2010 / 14:41:15 / cg"
 ! !
 
+
+
 !Block methodsFor:'accessing'!
 
 home
@@ -877,24 +881,32 @@
 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
+    overheadCycles := endCycles - startCycles.
     
     startTime := OperatingSystem getMicrosecondTime.
+    startCycles := OperatingSystem getCPUCycleCount.
     self value.
+    endCycles := OperatingSystem getCPUCycleCount.
     endTime := OperatingSystem getMicrosecondTime.
 
     micros := (endTime - startTime - overhead) max:0.
-
+    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 +916,23 @@
             Transcript show:(TimeDuration milliseconds:millis).
         ].
     ].
+    cycles ~~ 0 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:'
     "
 ! !
 
@@ -3396,7 +3420,8 @@
      a long return), evaluate the argument, aBlock.
      This is used to make certain that cleanup actions (for example closing files etc.) are
      executed regardless of error actions.
-     Same as the more modern #ensure:"
+     Same as the more modern, ANSI standardized #ensure:, 
+     which should be used instead for portability."
 
     <exception: #unwind>
 
@@ -3418,10 +3443,10 @@
 
      f := 'Makefile' asFilename readStream.
      [
-	l := f nextLine.
-	l isNil ifTrue:[^ 'oops']
+        l := f nextLine.
+        l isNil ifTrue:[^ 'oops']
      ] valueNowOrOnUnwindDo:[
-	f close
+        f close
      ]
     "
 
@@ -3432,8 +3457,9 @@
     "evaluate the receiver - when some method sent within unwinds (i.e. does
      a long return), evaluate the argument, aBlock.
      This is used to make certain that cleanup actions (for example closing files etc.) are
-     executed regardless of error actions
-     Same as the more modern #ifCurtailed:"
+     executed regardless of error actions.
+     Same as the more modern, ANSI standardized #ifCurtailed:, 
+     which should be used instead for portability."
 
     <exception: #unwind>
 
@@ -3449,23 +3475,23 @@
 
      s := 'Makefile' asFilename readStream.
      [
-	^ self
+        ^ self
      ] valueOnUnwindDo:[
-	Transcript showCR:'closing the stream - even though a return occurred'.
-	s close
+        Transcript showCR:'closing the stream - even though a return occurred'.
+        s close
      ]
     "
     "
      [
-	 |s|
-
-	 s := 'Makefile' asFilename readStream.
-	 [
-	    Processor activeProcess terminate
-	 ] valueOnUnwindDo:[
-	    Transcript showCR:'closing the stream - even though process was terminated'.
-	    s close
-	 ]
+         |s|
+
+         s := 'Makefile' asFilename readStream.
+         [
+            Processor activeProcess terminate
+         ] valueOnUnwindDo:[
+            Transcript showCR:'closing the stream - even though process was terminated'.
+            s close
+         ]
      ] fork
     "
 ! !