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. XMPP service, Jabber

Hi,
I am currently working on a project which requires a server to request
data from the devices which are currently connected to the server.
Meaning, first the server finds out which devices are connected to the
server and then sends an application specific message to the client
device which should wake certain functionality present on the client,
like for example send out its current location.

I was looking into XMPP service which looked promising since it works
as a PUSH notification. The <presence> in XMPP can be used to check if
the device is currently connected and has subscribed to the server.
Since every android user has a gmail account, I was thinking of using
the gmail xmpp service to connect /subscribe to the server. Then send
specific message to the client, i.e x...@gmail.com. But if a message is
sent this way the client app gets the message as well as the native
gtalk client. So how do I go about intercepting this so that it
doesn't show up on the gtalk client but my app still gets it.

I want the PUSH notification to work, not PUSH.

Please do comment on the above approach. If there is another way to do
this, please do suggest.


- SLY

-- 

2. REG: CSS style sheets

Can we import the CSS style sheets in to our android application.

-- 

3. increase the swipe more sensitive

4. Kenangan masa kecil

5. Emulator error : No space left on device

6. Some MapView mysteries

7. How to solve the problem of launch the apps twice or more?