Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increase performance of following code?

I have following code that displays an Image with letters,

public class MainActivity extends Activity 
{
    private String[] capital_letters,small_letters;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        try
        {
            capital_letters = getApplicationContext().getResources().getStringArray( R.array.capital_letters );
            small_letters = getApplicationContext().getResources().getStringArray( R.array.small_letters );

            MyAdapter adapter = new MyAdapter ( MainActivity.this ,capital_letters,small_letters );
            ListView list = ( ListView ) findViewById( R.id.list );
            list.setAdapter( adapter );

            list.setOnItemClickListener( new AdapterView.OnItemClickListener() 
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id ) 
                {
                    Intent intent = new Intent ( MainActivity.this, LetterActivity.class );
                    intent.putExtra( "position", position );
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    overridePendingTransition( 0, 0 );
                }
            });
        }
        catch ( Exception e ){e.printStackTrace();}
    }
}

How can I make my code such a light weight that it works very smoothly. Smooth performance meaning, Proper declaration of variable, loops, Garbage Collection usage etc ?

like image 582
Lucifer Avatar asked Jan 23 '12 09:01

Lucifer


2 Answers

You're not alone in thinking to get performance you need to declare variables properly, optimize loops, etc.

But if you just sit down and do that, you end up fixing things that aren't broken.

The key to performance tuning is this: first find out what needs to be fixed.

That's what profilers are supposed to help you do. Personally, I use random pausing, and so do a lot of people. Here's a short PDF slide show about that.

The deal is, unless your code is really simple (in which case it might not have much room for speedup), it probably has multiple things you can fix to speed it up. Suppose there are three:

  • A, responsible for 40% of the total time
  • B, responsible for 20% of the total time
  • C, responsible for 10% of the total time

OK, so first you find A, and you fix it. How much speedup do you get? (1/(1-.4)) = 1.67 or 67%. Not bad.

But if you look again at the percentages, now:

  • B takes 33% (because it takes as much time as before, but the total is no longer being puffed up by A)
  • C takes 17%

So they should be easier to find, since they take larger percents. You get the idea.

If you fix all of them, how much speedup do you get? 1/(1 - .7)) = 3.33 = 233%.

If there were more, like D and E, which there could very well be, you could go even further.

But if you miss any one of them, how much speedup do you get? That item becomes your speed limiter. So keep going until you can't find anything more.

Go to it, and have fun!

like image 102
Mike Dunlavey Avatar answered Oct 31 '22 22:10

Mike Dunlavey


It depends too much on your specific app to give a general answer. But Google has written a performance guide with Android-specific tips. I'd suggest reading that.

Of course it does not go much into general performance tips, but I expect you already know those. If not you should try to find some resources on general performance-tuning of programs and performance tips specific to Java, although not all of the latter will be applicable for Android.

Do not that while a lot of those tips do (very slightly) improve performance they might not always be the best decisions from a Software Design (e.g. maintainability and portability) point of view. Also a lot of them are simply "tricks" that can be automated by a compiler and thus will likely be incorporated into a later version of the JIT. They might even already be in 4.0 since the guide was written for 2.3.

like image 23
dtech Avatar answered Oct 31 '22 22:10

dtech