[Pharo-project] could we agree to remove caseOf: and caseOf:otherwise:

Sven Van Caekenberghe sven at beta9.be
Sun Feb 13 10:28:51 CET 2011


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.

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.

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: 

(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





More information about the Pharo-project mailing list