switch / if statement, efficiency question

by asymmetric » Tue, 28 Apr 2009 19:33:44 GMT

Sponsored Links

by looking at the Notepad examples, i've noticed that the switch
statement is used even when there's only one case handled, as in:

public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case INSERT_ID:
            return true;

        return super.onMenuItemSelected(featureId, item);

my question is, which is the most efficient statement? are there any
noticeable differences, enough to justify a refactoring?

thanks a lot!

switch / if statement, efficiency question

by fadden » Wed, 29 Apr 2009 04:37:31 GMT



For a single element, an "if" statement will be faster and more

Unless you're calling it thousands of times per second, it's not going
to make a difference in performance.  It's probably using about 20
more bytes of Dalvik bytecode than the equivalent "if", so unless you
have a bunch of them the size won't matter either.

Having all of the code look roughly the same may make it easier to
understand and maintain.


Sponsored Links

switch / if statement, efficiency question

by asymmetric » Wed, 29 Apr 2009 17:22:10 GMT

 fadden, thanks for your reply.

i'm curious as to why the android devs only used switch statements
then, if, as you say, an if statement would have been more efficient..

anyway, thank you!


switch / if statement, efficiency question

by Steve » Wed, 29 Apr 2009 21:35:23 GMT

 This would be a good setup if you were planning on adding more menu
items later, such as About or Settings or whatever.


switch / if statement, efficiency question

by fadden » Thu, 30 Apr 2009 04:20:05 GMT


Actually it's not.  Both Java and Dalvik have "sparse" and "packed"
switch instructions at the bytecode level.  You can read about them in
the Dalvik bytecode documentation in dalvik/docs/dalvik-bytecode.html
(packed-switch/sparse-switch, corresponding to tableswitch/
lookupswitch in Java).

Not really.  For a "packed" switch, which is essentially a goto table,
everything is the same speed.  For a "sparse" switch, the VM can
either do a linear search or a binary search through the table,
because the entries are sorted by the "case" value.  Note also that
the sorting makes rearranging entries in the source code meaningless.


Other Threads

1. How to get the simulator working

hey im trying to start the simulator to see the hello world app to get
a breif understanding but the window opens up and then closes.
does ne1 kno how i can fix this as i have followed the steps in get
started after downloading including setting the path.
i have no idea n hav been trying for aged
Help would be appreciated
Thanks T


2. What happened to M3's google.googlenav.Placemark and google.googlenav.Search?

This one goes back to M3.
google.googlenav.Placemark and google.googlenav.Search were retired
and replaced with a mock provider in M5, if I remember correctly.
I have to admit that I never bothered migrating location based search
coming out of M3 and lost sight of where these features finally
settled in the API. Has this functionality been resurrected in the
current release, and if so, where are the relevant parts?


3. Pinkal SHAH is out of the office.

4. aapt tool

5. TabWidget drawables?

6. new forum for android

7. Playing Sounds, What is the fastest? PCM MP3?