• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

What's more performant than a switch statement?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, thanks a lot William!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll try that.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great suggestion Daryl,
Could you provide an example of your suggestion?
Thanks, your the best.
Yoo-Jin.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I would like to see an example too.
Thanks
Shilpa
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic