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

  • Home
  • SEARCH
  • 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 9125141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:44:00+00:00 2026-06-17T06:44:00+00:00

After reading several articles on how to fine-tuning my code, I found out that

  • 0

After reading several articles on how to fine-tuning my code, I found out that the way we are declaring objects and variables could drasticatly impact the performance of our application. This is more and more important as time constraint is now an integral part of some aspects of the Android platform (e.g. result must be provided within 5 sec for some operations).

Now, I found the following code in one of my fragment class (perfectly functional):

Activity myA = getActivity();
if(myA instanceof MainActivity) {
((MainActivity) myA).doNext();
}

Knowing that objects creation/destruction and ressources allocation are some of the factors contributing to drain-out the device’s batteries, I had in mind to rewrite my code to:

if(getActivity() instanceof MainActivity) {
((MainActivity ) getActivity()).doNext();
}

From a functional perspective, both codes are delivering the same results. However, what is the most suitable approach? I’m asking because I see pros and cons to both approach and I’m unable to find the appropriate way to evaluate the performance and I’m also not clear on which counters (memory usage, function’ speed, used CPU cycles, etc) the evaluation should be performed.

Thanks for your contribution in advance.

  • 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-17T06:44:04+00:00Added an answer on June 17, 2026 at 6:44 am

    Guessing what could be a performance bottleneck is a very common waste of time. You have to measure what makes a difference to your whole program instead of guessing.

    Knowing that objects creation/destruction is one of the factors

    Creating a few objects doesn’t matter. Creating too many object matters. How much is too much and which objects are created most is something you should use a profiler to determine.

    From a functional perspective, both codes are delivering the same results. However, what is the most suitable approach?

    Whatever you feel is the simplest. I believe that adding the method doNext to the class which getActivity() returns would be best so you can write the following is clearest.

    getActivity().doNext();
    

    I’m unable to find the appropriate way to evaluate the performance

    Use a CPU and memory profiler, or write a performance test for different components of your system.

    which counters (memory usage, function’ speed, used CPU cycles, etc) the evaluation should be performed.

    If you can’t use a profiler, I would start simple with elapse time. i.e. System.nanoTime(). This is usually all you need.

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

Sidebar

Related Questions

After reading several questions about .gitignore I have found no one that could answer
After reading several articles of a hierarchical data tables, I've made my way into
After reading several articles on jms connection pooling I got confused. Could one please
After reading several other blog posts and articles (references found below) there appear to
After reading several questions regarding problems with compilation (particularly C++) and noticing that in
After reading several tutorials I came up with the following code: (Using the famous
I puzzled with understanding ADO.NET, after reading several articles I do not have a
After reading several posts on this matter, I thought I was all clear about
Short 2 questions, I cannot find clear answers after reading several topic/pages. If I
I was attempting to use :last-child with IE8, but after reading several threads about

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.