On Sun, 13 Feb 2011, Sven Van Caekenberghe wrote:
On 12 Feb 2011, at 18:41, Levente Uzonyi wrote:
~27x slowdown in this case.
I personally never heard of #caseOf:otherwise !
It feels like a hack that is not often needed.
You don't need it often, but it's sometimes useful.
If after writing correct code you need to optimize integer/byte handling, there are many solutions, check any book on algorithms, most of them are using table dispatch.
The blocks of #caseOf: are not just boilerplate code, you can evaluate any expression there. It's cumbersome to do that with table based dispatch.
Another problem in your benchmark is that you keep the creation of the dynamic array inside the code to measure, while in the compiler primitive case it is of course compiled away, that alone makes a huge difference:
It's not a problem, it's intentional. See the mail that I replied to. Levente
(timings on my machine)
[ 1 to: 10 do: [ :each | each caseOf: { [ 1 ] -> [ $a ]. [ 2 ] -> [ $b ]. [ 3 ] -> [ $c ]. [ 4 ] -> [ $d ]. [ 5 ] -> [ $e ] } otherwise: [ $x ] ] ] bench. '8,920,000 per second.'
[ 1 to: 10 do: [ :each | ({ [ 1 ] -> [ $a ]. [ 2 ] -> [ $b ]. [ 3 ] -> [ $c ]. [ 4 ] -> [ $d ]. [ 5 ] -> [ $e ] } detect: [ :ea | ea key value = each ] ifNone: [ [ $x ] ]) value ] ] bench. '70,600 per second.'
| actions | actions := { [ $a ]. [ $b ]. [ $c ]. [ $d ]. [ $e ]. [ $x ]. [ $x ]. [ $x ]. [ $x ]. [ $x ]. }. [ 1 to: 10 do: [ :each | (actions at: each) value ] ] bench. '3,230,000 per second.'
| letters | letters := 'abcdexxxxx'. [ 1 to: 10 do: [ :each | letters at: each ] ] bench. '8,930,000 per second.'
If other Smalltalks can live without it, so can we.
Sven