1

Here is the piece of code I'm questioning about

        for (int i = 0; i < this.options.size(); i++) {
            RadioButton butt = this.options.get(i);
            //do something with butt
        }

would I gain a huge performance improvement if I changed it to:

        RadioButton butt;
        for (int i = 0; i < this.options.size(); i++) {
            butt = this.options.get(i);
            //do something with butt
        }

EDIT: how about if this code is to be executed 30-50 times a second with options being around size 20?

7
  • huge ? no. the only difference is that there would be 1 memory allocation for 1 pointer instead of 1 for each item of the loop. given this is a ui element, you probably have at top most a few dozens, which makes it totally negligible. Commented Nov 7, 2013 at 20:36
  • When you observed that your application's performance was not meeting your performance requirements, and you profiled your application, is this where you determined the bottleneck was? What was the measurable difference in your testing? Commented Nov 7, 2013 at 20:44
  • @Jason I haven't tested my application for performance. I'm just curious to see which method is faster (apparently there is not much difference). Anyways, I searched on Google and on StackOverflow but found no answers, that's why I posted. Commented Nov 7, 2013 at 20:47
  • 1
    concerning your edit : you should be more worried about why do you need to loop 50 times per sec on a 20 items list than about should the declaration be in or out. Commented Nov 7, 2013 at 20:55
  • Answer your edit: Same in both cases again. See my answer. Commented Nov 7, 2013 at 20:56

6 Answers 6

4

For all realistic, measurable cases, there is absolutely no difference between the two performance wise. In fact, I'm pretty sure (admittedly I don't know for sure) they result in the exact same number of assignments and reference creations. It would be stupid for the JVM to create N number of reference holders. It would simply reuse the one created during the first iteration, simply giving it the reference in the next assignment. Which means only one reference holder is used for both cases (assuming this is true).

Sign up to request clarification or add additional context in comments.

1 Comment

I guess that makes it 0 difference due to smart compiler optimizations.
3

You're not creating objects here, you're just creating references, and whether you're creating one reference or more doesn't really matter.

Comments

3

Looking at the title, I knew this was going to be yet-another-misguided-performance-question.

A couple of things:

  • No, those are virtually identical except for the scope of the variable.
  • In general, if you're worried about micro-optimizations like that, you're spending your time on entirely the wrong thing. In this case it's moot since there is no difference, but even if you were talking about e.g. one assignment:
    1. The difference is nanoseconds and completely negligible compared to other things you are doing.
    2. The compiler is much smarter than you about optimizing.
    3. The JVM interpreter and hotspot compiler are far smarter than you as well.
  • If you haven't set clear performance requirements, and you haven't determined that your code does not meet those requirements, and you haven't profiled your code and determined where the bottleneck is, you have no business asking optimization questions like this.

As for the GC comment you made in another answer: The GC happens in the background, is intelligent, and makes decisions that you have absolutely zero control over (aside from JVM command line tuning -- don't get excited, based on the fact that you asked this question, you probably aren't equipped to make good decisions about tuning parameters). Moving the reference from one place to another gives you no significant measure of control over how the GC handles it. Each time through the loop the previous reference is no longer reachable, the GC will clean it up at an undefined point in the future.

6 Comments

I think there is a difference, since RadioButton butt results in a memory allocation for a reference, therefore putting the declaration outside the loop can result in less allocations. In practice, the compiler is probably smart enough to do that himself. And allocating a reference slot is really not a big deal.
You cannot say that that results in a "memory allocation for reference". Is it in a register? On the stack? In a local stack pool? Look at the resulting byte code. More importantly, does it matter? Don't mislead the OP by suggesting that even the worst-case scenario here is a reasonable target for optimization. This is a blatant and classic case of misguided, premature micro-optimization, and to even suggest that there is substance to it is a disservice to the OP and to any similarly minded readers that may see this.
As a new programmer its really difficult to know if you are doing something poorly or not, or if one of many ways of doing something is better. This is a great answer to that question, and would be even better without the belittling quips.
I agree with the whole no unless micro optimization part. I just think that writing Object a; create a reference that does occupy 4 bytes of heap.
The belittling quips are out of love.
|
2

I think code and performance is almost same only looks different. You are not creating new instances but only copy references of objects from your collection.

But i like and usually use second approach.

8 Comments

So after every loop, the reference in that loop gets recycled immediately? And then in the next loop I'm making another reference to a object. SO from this logic the second method would be faster since it is only assigning the reference to the temp reference, i.e. less work for GC, less memory allocation?
The GC happens in the background, is intelligent, and makes decisions that you have absolutely zero control over. Moving the reference from one place to another gives you no significant measure of control over how the GC handles it. Each time through the loop the previous reference is no longer reachable, the GC will clean it up at an undefined point in the future.
@nevercode i would say almost same as you but it also depends on behaviour of GB, compiler etc but difference won't be huge.
@nevercode : are you really worried about when a bunch of references are going to be collected ? a reference is 4 bytes.
@nevercode Are you serious? 30-60 times a second, for one, is a crawl. Do you believe the order of magnitude of any such difference would even approach the ~20ms per frame you're speaking of? The few nanoseconds your speaking of here is on the order of 1/10,000,000 the fraction of that. Secondly, how do you think the size of the array affects anything here? Do you know how array lists work?
|
0

The difference is not huge as assignment of the object is the biggest cost here. Also, the compiler will make your code more efficient, so in the end it is the same cost in performance.

Comments

0

In both cases you are creating the RadioButton object in the loop because RadioButton butt it's only a refernce and not an instance of the object. Presumably is this.option.get(i) which creates your object. So my answer is: no.

The only thing that changes is that in the second loop you're creating this.options.size()-times the reference butt

2 Comments

presumably, options is a List, therefore get retrieves an element, but does not create it. moreover, get is rarely a method name used to indicate that something is created.
Yes, now make it sense. In this case he's linking only a previous instanced object with a new reference. So he isn't creating a new object at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.