[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [sc-users] A bug in switch/case?
On Dec 29, 2004, at 3:43 AM, Alberto de Campo wrote:
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):
Your comparison is not valid.
In your first example you present a switch statement that is not in a
form that can be inlined. Thus it has to build the dictionary each time
in the loop instead of at compile time.
If you change it to the inlined form then it is faster than your second
example.
The following runs faster than your two examples:
(
var z;
z = [0, 1, 1.1, 1.3, 1.5, 2, 3, 4, 5];
{
100000.do {
z.choose.switch (
1, { \no },
1.1, { \wrong },
1.3, { \wrong },
1.5, { \wrong },
2, { \wrong },
0, { \true },
3, { \true });
}
}.bench;
)
secondly, most of the time you have a situation where a loop calls a
method and in that method you must build a dictionary of cases. In this
situation, you can't move the dictionary outside the loop since it is
hidden in the method, thus it is better to have built that dictionary
at compile time.
(
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.