atom feed40 messages in org.codehaus.groovy.userRe: [groovy-user] Acceptable Placemen...
FromSent OnAttachments
Randall R SchulzDec 3, 2007 3:41 pm 
Stand TrooperDec 3, 2007 4:03 pm 
Randall R SchulzDec 3, 2007 4:07 pm 
Stand TrooperDec 3, 2007 4:34 pm 
Russel WinderDec 3, 2007 9:46 pm 
Russel WinderDec 3, 2007 9:49 pm 
Charles Oliver NutterDec 3, 2007 9:52 pm 
Randall R SchulzDec 3, 2007 10:10 pm 
Jochen TheodorouDec 4, 2007 5:59 am 
Charles Oliver NutterDec 4, 2007 10:59 am 
Jochen TheodorouDec 4, 2007 11:51 am 
Smith, Jason, CTR, OASD(HA)/TMADec 4, 2007 1:10 pm 
Charles Oliver NutterDec 4, 2007 1:24 pm 
Charles Oliver NutterDec 4, 2007 1:29 pm 
Jochen TheodorouDec 4, 2007 1:54 pm 
Smith, Jason, CTR, OASD(HA)/TMADec 4, 2007 2:33 pm 
Jochen TheodorouDec 4, 2007 2:44 pm 
Charles Oliver NutterDec 4, 2007 2:56 pm 
Smith, Jason, CTR, OASD(HA)/TMADec 5, 2007 6:35 am 
Charles Oliver NutterDec 5, 2007 10:16 am 
Smith, Jason, CTR, OASD(HA)/TMADec 5, 2007 10:29 am 
Jochen TheodorouDec 5, 2007 5:45 pm 
Jochen TheodorouDec 7, 2007 8:31 am 
Randall R SchulzDec 7, 2007 8:37 am 
Jochen TheodorouDec 7, 2007 8:49 am 
Guillaume LaforgeDec 7, 2007 9:01 am 
Jochen TheodorouDec 7, 2007 9:14 am 
Guillaume LaforgeDec 7, 2007 9:16 am 
Randall R SchulzDec 7, 2007 9:22 am 
Guillaume LaforgeDec 7, 2007 9:28 am 
Charles Oliver NutterDec 7, 2007 10:09 am 
Charles Oliver NutterDec 7, 2007 10:11 am 
Randall R SchulzDec 7, 2007 10:18 am 
Jochen TheodorouDec 7, 2007 10:31 am 
Charles Oliver NutterDec 7, 2007 12:23 pm 
Randall R SchulzDec 7, 2007 12:37 pm 
Charles Oliver NutterDec 7, 2007 1:35 pm 
Randall R SchulzDec 7, 2007 1:40 pm 
Gavin GroverDec 7, 2007 11:49 pm 
Jochen TheodorouDec 8, 2007 3:40 am 
Subject:Re: [groovy-user] Acceptable Placement of default: Within Switch?
From:Jochen Theodorou (blac@gmx.org)
Date:Dec 4, 2007 11:51:38 am
List:org.codehaus.groovy.user

Charles Oliver Nutter schrieb:

Jochen Theodorou wrote:

if you do a micro benchmark of the performance between the to switch forms in Java (in bytecode) you will most probably not see a big difference between them. and they will probably behave just the same as a normal if-else cascade. Of course this is onlytrue on certain VMS, on others it is faster/slower, ut by such a small amount... its not worth it I say.

My understanding was that Groovy's switch is not a binary search, but a series of invoke/compare/jumps. That would be considerably slower than a direct jump table or a binary search. CS 101: a series of ifs will be O(n), a binary search will be O(log n), and table lookup O(1). table lookup is faster, so I believe it would be worth it.

ok, let us do less speculation and more concrete talk. There are three ways to implement a switch case.

1) direkt jump (tableswitch, O(1)): We calculate an offsett adress depending on the switch value. This will allow us to directly jump to our goal, but requires that the case values are not too far scattered.

2) binary search (lookupswitch, O(logn)): We assume we have a sorted list of case values and can do a binary search on this list by comparing the case vale and the switch value.

3) if-else-cascade (O(n)): we go step by step through each case value.

From looking only at the numbers here the tableswitch seems to be the best choice, but the restrictions on my data are very high. not to forget that I need to calculate the offset. usually this is pretty fast, but should not be forgotten. the lookupswitch is less restrictive, and does not really need a number to work. But it needs a sorted List with case-value/jump-goal pairs. Theoretically you could sort the list before doing the lookup, but what is the point in doing a O(nlogn) action instead of a O(n) action? Not to forget, that if you sort by using the case values directly you might cause side effects, that where intended to be triggered only for matches. So unless you can avoid the side effects and cache the list (for example let the compiler do the sorting if possible) this version might be worse than the cascade. In Java a value compatible with int is required, so it is very easy. But because of the calculation a lookupswitch of size 1 might be faster than a tableswitch of size 1. Well, probably as fast as a if-else then. How many cases are needed to make the tableswitch really faster depends on how much time it takes to calculate the offset.

Now I don't know about Ruby, but in Groovy we have a isCase method, that will be called to see if a case matches. This automatically means we can not do the switch constructs, unless we know the case values before and there is a sorting that matches them. So a switch over a character where I only match with character constants could be done quite easily with 1) or 2), but the general case can not. A case may in Groovy also include ranges, closures or regexp... that's not good for sorting and not good for your cases if they depend on a certain order in your code. For example:

switch (x) { case Integer: println "I am Integer" break; case Object: println "I am Object" break; case default: println "Hell, what am I?" }

if you use this code in Groovy with for example 5 as value for x, then it must print the Integer text. My sorting would now have to know this! Put an interfaces in there and you get a hell of doing a correct sorting.

So the short answer to why Groovy does not do this is: because it is not trivial to do for something else than numbers.

When talking about the O-Notation we always talk about very big n, our usual used n are not that big. So the three have to be tested in real world cases to see which of them is the fastest. And which of them it is might depend on your VM, your CPU and the mood of the weather. But all in all... will the if-else be much slower than the other two? What for example if my second case would match, but I have a very slow isCase implementation on the median of my search list? The call to isCase on median may outrank x other isCase calls on the case values before.

So the longer answer is: In real world applications the lookup and table switches might not be much faster than a normal if-else, because there are not enough values to make a real difference. It makes a difference only in certain cases and these are currently no uses cases for Groovy. so we may optimize it in the future, but currently there is no need.

bye blackdrag