switch / if statement, efficiency question

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


Sponsored Links
 hi!

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:
            createNote();
            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!
asymmetric
--~--~---------~--~----~------------~-------~--~----~



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
compact.

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..
hmm..

anyway, thank you!
asymmetric





--~--~---------~--~----~------------~-------~--~----~



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. Paging: Alexbong (urgent)

Bro Alexbong, bisa tolong add gTalk saya? Add di: rc.milest...@gmail.com
Ada yg mau saya bicarakan. Thanks yah

--rc
Cyanogen(mod) 5.0.3-N1

-- 
"Indonesian Android Community [id-android]" 

2. How to start another application from my activity.



http://developer.android.com/intl/de/guide/appendix/g-app-intents.html

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Development Wiki: http://wiki.andmob.org

-- 

3. how to change the content of a TextView

4. How to start another application from my activity.

5. Unexplained error from MediaPlayer

6. Non-parallel construction of DatePicker and TimePicker

7. Layout resource: FWVGA vs. WVGA