[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] A bug in switch/case?



Hi Ph,

It is possible to use additional switch structures in case there are more than 7 expressions to be evaluated, but I wonder what is the advantage of using "case" or "switch" instead of an IdentityDictionary. ??

AFAIR, people wanted to have switch, even against the advice that
dictionaries are better OOP style and faster. I just measured speed
again (on a PB G4/1.33 GHz):

(
var z, cases;
z = [0, 1, 1.1, 1.3, 1.5, 2, 3, 4, 5];
cases = [ 1, { \no },
1.1, { \wrong },
1.3, { \wrong },
1.5, { \wrong },
2, { \wrong },
0, { \true },
3, { \true }
];
)
{
100000.do {
	z.choose.switch (*cases);
}
}.bench;
)

-> ca 1.4 to 1.75 sec.

(
var z, cases;
z = [0, 1, 1.1, 1.3, 1.5, 2, 3];
cases = IdentityDictionary[
	1 -> { \no },
	1.1 -> { \wrong },
	1.3 -> { \wrong },
	1.5 -> { \wrong },
	2 -> { \wrong },
	0 -> { \true },
	3 -> { \true }
];

{
100000.do {
	cases[z.choose].value;
}
}.bench;
)

-> ca 0.33 to 0.45 sec, and the same
same with Event instead of IdentityDictionary.

best,
adc
--
--
Alberto de Campo
Bergstrasse 59/33
A-8020 Graz, Austria
e-mail : decampo@xxxxxx
--