Skip to content Skip to sidebar Skip to footer

Creating Objects Inside Loop Vs. Creating One Temp Object Before Loop

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 some

Solution 1:

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).

Solution 2:

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

Solution 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.

Solution 4:

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.

Solution 5:

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.

Post a Comment for "Creating Objects Inside Loop Vs. Creating One Temp Object Before Loop"