Benchmarking in Pharo

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

Common Wisdom

If you did not profile your code you may have 40-50% speed up waiting for you.

Measuring Execution Speed

We create an expression and use [ expression ] timeToRun

[ 1000 factorial ] timeToRun

Comparing Two Executions

Is select:, then collect: slower than select:thenCollect:?

timeToRun example

| coll |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection.
[ 1000000 timesRepeat: [ 
		(coll select: [ :each | each > 5]) collect: [ :i | i * i ]]
	] timeToRun
>  "0:00:00:00.517"
| coll |
coll := #(1 2 3 4 5 6 7 8 9 10) asOrderedCollection.
[ 1000000 timesRepeat: [ 
		coll 
			select: [ :each | each > 5]
			thenCollect: [ :i | i * i ]]
		] timeToRun 
> "0:00:00:00.362"

bench

[ 1000 factorial ] bench 
> '610.234 per second' 

The higher the better!

[ 1234 factorial ] benchFor: 2 seconds

Time Profiler

TimeProfiler 
  spyOn: [ 20 timesRepeat: [ 
            Transcript show: 1000 factorial printString ] ]

Time Profiler

Summary

/