Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:11:41+00:00 2026-06-12T17:11:41+00:00

I’m having to think hard about optimisation in an Android app for the first

  • 0

I’m having to think hard about optimisation in an Android app for the first time.

I have searched, but the more I search, the more confused I get. Does anyone know of good documentation to describe how each of the Java and JVM components optimise WRT JAVAC bytecode and Dalvik bytecode? Is most of it done by Dalvik and is therefore “black boxed” away from me? Is it worth looking at?

Some trivial examples.

SharedPreferences settings = getSharedPreferences(MY_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefeditor.putString("FOO", "BAR"); 
prefEditor.commit();

vs

getSharedPreferences(MY_PREFERENCES, MODE_PRIVATE).settings.edit().putString("FOO","BAR").commit();

or

int a, b, c;

a = 2;
b = 3;

c = sum(a,b);

int sum(int x, int y){return x + y;}

vs

c = a + b;

My reason for asking is to understand the tradeoff between readability and maintainability and performance. I know that the standard answer is “measure it” but I am trying to move beyond that and think in advance where I can gain performance rather than have to go back into complex code and refactor based on metrics. I will of course measure and profile anyway but my question is about understanding.

I am open minded enough to accept “don’t even go there, write, measure and be damned”!

Thanks for any insights.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:11:43+00:00Added an answer on June 12, 2026 at 5:11 pm

    For an android application, your code goes through 2 compilation/optimization passes. The first is performed by javac, when you compile the java code to java bytecode. The 2nd is performed by dx, when you translate the java bytecode to dalvik bytecode.

    I’m not familiar with any documentation that describes the optimizations performed by either. But both are open source (via openjdk, for javac), so you could theoretically examine both to get a feel for what optimizations they do.

    However, your examples really have nothing to do with compiler level optimizations. I don’t think knowing how, e.g., dx optimally allocates registers will help you to understand how to write performant java code.

    For the two sets of examples that you mention, I would suggest trying 2 things on both sets of examples.


    1. Disassemble

    The first thing would be to disassemble the dalvik bytecode, and take a look at any differences. Simply counting the instructions won’t necessarily tell you anything, but in some cases it is clear that one is faster than the other.

    For example, in your first set of examples, the bytecode for both snippets is nearly identical, with the exception that the 2nd snippet contains an additional instruction. So the first snippet is clearly more performant, although it would be an extremely minor performance difference.

    invoke-interface {v0, v1, v2}, Landroid/content/SharedPreferences$Editor;->putString(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;
    invoke-interface {v0}, Landroid/content/SharedPreferences$Editor;->commit()Z
    

    vs.

    invoke-interface {v0, v1, v2}, Landroid/content/SharedPreferences$Editor;->putString(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;
    move-result-object v0
    invoke-interface {v0}, Landroid/content/SharedPreferences$Editor;->commit()Z
    

    This extra instruction makes sense, if you take a close look at the two java snippets. In the first, the return value of putString is ignored, and commit is called on the same value that you called putString on. While in the 2nd snippet, clone() is called on the value returned by putString(), which requires an extra operation in the dalvik bytecode to save off the return value. Namely, the extra move-result-object instruction that you see there.

    We, as the programmers, know that the return value of the putString method is the same object that is is being called on. However, the compiler can’t know or assume this.


    2. Measure

    The second thing you can do is, as you mention, to actually profile them and measure the performance. After a while, you should start to get a feel for the types of things you can do to improve performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.