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. How to set the height of Preference or PreferenceCategory

Hi, all
    I just met a problem, when I use PreferenceScreen composed by
several PreferenceCategory which composed by several Preference. And I
found it can't set the max height of the Preference, that means, I
wrote 500 char, it just show 50 char, and cut the left. Can u help me?

    Thank u

-- 

2. problems with custom listview adapter getView() method

this is a cross-post from SO . haven't got much attention over there
so I am posting it here. orig here:

http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-coh

----------------------------------------------------------------------------------

I have a custom list adapter:

class ResultsListAdapter extends ArrayAdapter<RecordItem> {

in the overridden 'getView' method I do a print to check what position
is and whether it is a convertView or not:

    @Override
    public View getView(int position, View convertView, ViewGroup
parent) {
        System.out.println("getView " + position + " " + convertView);

The output of this (when the list is first displayed, no user input as
yet)

04-11 16:24:05.860: INFO/System.out(681): getView 0 null
04-11 16:24:29.020: INFO/System.out(681): getView 1
android.widget.relativelay...@43d415d8
04-11 16:25:48.070: INFO/System.out(681): getView 2
android.widget.relativelay...@43d415d8
04-11 16:25:49.110: INFO/System.out(681): getView 3
android.widget.relativelay...@43d415d8
04-11 16:25:49.710: INFO/System.out(681): getView 0
android.widget.relativelay...@43d415d8
04-11 16:25:50.251: INFO/System.out(681): getView 1 null
04-11 16:26:01.300: INFO/System.out(681): getView 2 null
04-11 16:26:02.020: INFO/System.out(681): getView 3 null
04-11 16:28:28.091: INFO/System.out(681): getView 0 null
04-11 16:37:46.180: INFO/System.out(681): getView 1
android.widget.relativelay...@43cff8f0
04-11 16:37:47.091: INFO/System.out(681): getView 2
android.widget.relativelay...@43cff8f0
04-11 16:37:47.730: INFO/System.out(681): getView 3
android.widget.relativelay...@43cff8f0

AFAIK, though I couldn't find it stated explicitly, getView() is only
called for visible rows. Since my app starts with four visible rows at
least the position numbers cycling from 0-3 makes sense. But the rest
is a mess:

    * Why is getview called for each row three times?
    * Where are these convertViews coming from when I haven't scrolled
yet?

I did a bit of reseach, and without getting a good answer, I did
notice that people were associating this issue with layout issues. So
in case, here's the layout that contains the list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/pageDetails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

</LinearLayout>

and the layout of each individual row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="108dp"
android:padding="4dp">

<ImageView
    android:id="@+id/thumb"
    android:layout_width="120dp"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp"
    android:src="@drawable/loading" />

<TextView
    android:id="@+id/price"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentBottom="true"
    android:singleLine="true" />

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:paddingRight="4dp"
    android:singleLine="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dp"
    android:layout_toRightOf="@id/thumb"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingRight="4dp"
    android:layout_alignWithParentIfMissing="true"
    android:gravity="center" />

</RelativeLayout>

Thank you for your time


-- 

3. Could someone help to explain the relationship among View, Window, Activity and WindowManager? Thansk your very much!

4. no file stored when i use FileOutputStream outStream.write(data);

5. emulator: ERROR: the user data image is used by another emulator. aborting (cont'd)

6. ScrollText inside button

7. emulator: ERROR: the user data image is used by another emulator. aborting