• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's more performant than a switch statement?

 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In another language (Clipper, Visual dBASE) we can store code blocks in an array, then do something like:
eval(arrayOfCodeBlokcs[caseNumber])
In cases where there are a lot of cases to check this is really fast!
Any idea to implement this idea in Java?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have a hard time beating the switch statement. Java builds a jump table to the code blocks so selecting a block should be quite fast.
Bill
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks a lot William!
 
Daryl Fong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can achieve similar by using a delegation model. In java, everything is view as object. Thus your code blocks are objects.
Now you have to implement a standard interface for all the objects. For each object implementing the interface you'll have to register them to a Factory class. U can use a hashMap to do that.
Now everytime you like to get a code block (object in Java), the Factory class will just return the Interface and hence you're able to invoke the method defined in the interface.
Hope this help.
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try that.
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great suggestion Daryl,
Could you provide an example of your suggestion?
Thanks, your the best.
Yoo-Jin.
 
Ipsita Naravane
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I would like to see an example too.
Thanks
Shilpa
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cute trick.
Those using it should understand the tradeoffs. For this to work, you need to have objects in your factory. Either these objects get created as needed (and may or may not be recycled, depending on whether or not they retain state), or they may all be created ahead of time, and stored.
In the former case, created JIT-style, you take an object creation performance hit, which may overshadow the savings from the switch. In the latter case, you take a startup hit, as well as increated memory use; this option makes sense when you will be reusing the "switch" repeatedly, and can recycle stateless objects from within the factory.
There are also maintenence and code size issues to consider.

Bottom line, don't naively assume a fancy trick like this will already be in your best interest. its clever and useful, but if you're not careful, you can get burned.
--Mark
hershey@vaultus.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!