Declare an object inside or outside a loop?

前端 未结 16 784
广开言路
广开言路 2020-12-07 17:15

Is there any performance penalty for the following code snippet?

for (int i=0; i

        
相关标签:
16条回答
  • 2020-12-07 17:49

    The first code is better as it restricts scope of o variable to the for block. From a performance perspective, it might not have any effects in Java, but it might have in lower level compilers. They might put the variable in a register if you do the first.

    In fact, some people might think that if the compiler is dumb, the second snippet is better in terms of performance. This is what some instructor told me at the college and I laughed at him for this suggestion! Basically, compilers allocate memory on the stack for the local variables of a method just once at the start of the method (by adjusting the stack pointer) and release it at the end of method (again by adjusting the stack pointer, assuming it's not C++ or it doesn't have any destructors to be called). So all stack-based local variables in a method are allocated at once, no matter where they are declared and how much memory they require. Actually, if the compiler is dumb, there is no difference in terms of performance, but if it's smart enough, the first code can actually be better as it'll help the compiler understand the scope and the lifetime of the variable! By the way, if it's really smart, there should no absolutely no difference in performance as it infers the actual scope.

    Construction of a object using new is totally different from just declaring it, of course.

    I think readability is more important that performance and from a readability standpoint, the first code is definitely better.

    0 讨论(0)
  • 2020-12-07 17:49

    The first makes far more sense. It keeps the variable in the scope that it is used in. and prevents values assigned in one iteration being used in a later iteration, this is more defensive.

    The former is sometimes said to be more efficient but any reasonable compiler should be able to optimise it to be exactly the same as the latter.

    0 讨论(0)
  • 2020-12-07 17:50

    You can disassemble the code with javap -c and check what the compiler actually emits. On my setup (java 1.5/mac compiled with eclipse), the bytecode for the loop is identical.

    0 讨论(0)
  • 2020-12-07 17:55

    Don't prematurely optimize. Better than either of these is:

    for(Object o : someList) {
        o.doSomething();
    }
    

    because it eliminates boilerplate and clarifies intent.

    Unless you are working on embedded systems, in which case all bets are off. Otherwise, don't try to outsmart the JVM.

    0 讨论(0)
  • 2020-12-07 17:55

    In both cases the type info for the object o is determined at compile time.In the second instance, o is seen as being global to the for loop and in the first instance, the clever Java compiler knows that o will have to be available for as long as the loop lasts and hence will optimise the code in such a way that there wont be any respecification of o's type in each iteration. Hence, in both cases, specification of o's type will be done once which means the only performance difference would be in the scope of o. Obviously, a narrower scope always enhances performance, therefore to answer your question: no, there is no performance penalty for the first code snip; actually, this code snip is more optimised than the second.

    In the second snip, o is being given unnecessary scope which, besides being a performance issue, can be also a security issue.

    0 讨论(0)
  • 2020-12-07 17:57

    In today's compilers, no. I declare objects in the smallest scope I can, because it's a lot more readable for the next guy.

    0 讨论(0)
提交回复
热议问题