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 830697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:05:11+00:00 2026-05-15T04:05:11+00:00

For a variable used in a function that is called very often and for

  • 0

For a variable used in a function that is called very often and for implementation in J2ME on a blackberry (if that changed something, can you explain)?

class X {
    int i;
    public void someFunc(int j) {
        i = 0;
        while( i < j ){
            [...]
            i++;
        }
    }
}

or

class X {
    static int i;
    public void someFunc(int j) {
        i = 0;
        while( i < j ){
            [...]
            i++;
        }
    }
}

or

class X {
    public void someFunc(int j) {
        int i = 0;
        while( i < j ){
            [...]
            i++;
        }
    }
}

I know there is a difference how a static versus non-static class variable is accessed, but I don’t know it would affect the speed. I also remember reading somewhere that in-function variables may be accessed faster, but I don’t know why and where I read that.

Background on the question: some painting function in games are called excessively often and even small difference in access time can affect the overall performance when a variable is used in a largish loop.

Update

I setup a small test (code below) and ran it on a real device to see what the results are like. I ran 10000 calls to a function that looped 10000 times accessing a variable.

  • if the variable was in-function it took ~9250 ms to run
  • if the variable belonged to the class, it took ~ 21700 ms to run
  • if the variable belonged to the class but was static it tool ~210000 ms to run.

I don’t know how relevant are the results of the test, if they would hold in a real-world program and if there is no other external factor at play. But, assuming they do, since it matches the most commonly held view here, then there is a sizeable difference between the access times.

Does this fall under premature optimizing? Maybe, but it also seems like a useful guideline to use in-function variable as much as possible. Even copying a class-variable to an in-function one seems like it could impact the execution time.

   final static int MAX = 10000;
   private void runTest()
   {
       long startTime = System.currentTimeMillis();
       for(int count = 0; count < MAX; count++)
           test1(MAX);
       test1.setText(""+(System.currentTimeMillis()-startTime));

       startTime = System.currentTimeMillis();
       for(int count = 0; count < MAX; count++)
           test2(MAX);
       test2.setText(""+(System.currentTimeMillis()-startTime));

       startTime = System.currentTimeMillis();
       for(int count = 0; count < MAX; count++)
           test3(MAX);
       test3.setText(""+(System.currentTimeMillis()-startTime));

   }

   void test1(int j)
   {
       int i = 0;
       while(i < j)
       {
           i++;
       }
   }
   int i2;
   void test2(int j)
   {
       i2 = 0;
       while(i2 < j)
       {
           i2++;
       }
   }
   static int i3;
   void test3(int j)
   {
       i3 = 0;
       while(i3 < j)
       {
           i3++;
       }
   }
  • 1 1 Answer
  • 1 View
  • 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-05-15T04:05:12+00:00Added an answer on May 15, 2026 at 4:05 am

    They have completely different semantics – do you want the value to be part of the state of the class, part of the state of instances of the class, or local to the method?

    The JIT has more opportunity to optimise the last version as it knows everything about where the variable might be used (and doesn’t need to worry about other threads accessing it). I’d say it’s likely to be the fastest.

    You should really test though – and in general, write the most readable code first and see whether you’ve actually got a bottleneck before trying this sort of micro-optimisation.

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

Sidebar

Related Questions

I used to believe that any variable that is shared between two threads, can
I know I can use a quasi-random number generation function/variable called Rnd. However, I've
Let's say I have a simple class called WebsterDictionary that has a function that
I often need to make a core function that's used in many places somehow
I've read that static variables are used inside function when one doesn't want the
The PhysicalAddress (used for MAC addresses) class supports variable-length byte arrays. As far as
I have a C++ unmanaged class NativeDog that needs to be used from C#,
I have an expensive function call that gets called once per loop, but I
I have a subprocess function (called parseContents) that gets called using the following code:
I have a function that takes another function as a parameter. Something like this

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.