Java – performance difference between post increment and pre increment operators?

In Java, does the use of post increment and pre increment operators have any performance impact?

Example:

i++

++i

Solution

Performance issues only make sense in the context of the same functional behavior (because if the functions are different, the correct behavior is better than the slightly faster behavior), so I assume you mean a situation where the value of the expression is not used? That is, the only purpose of the expression is to increase I? In this case, the answer is no: there is no performance difference, in fact, there is no difference I just compiled this class:

public class Foo
{
    public static void main(final String args[])
    {
        int i = Integer.parseInt(args[0]);
        i++;
    }
}

And calculated foo MD5 checksum of class; And, similar to the version of I They have the same checksum, indicating that the two versions are compiled into exactly the same bytecode, so they are literally the same

(of course, in theory, it may depend on the compiler. Different compilers may decide to compile I in different ways, even in the same context. But I doubt it, even if it's really not worth worrying about.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>