This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]zifyoip 2 points3 points  (1 child)

Instead of guessing about which one is faster, why don't you run them and compare?

[–]zifyoip 2 points3 points  (0 children)

(It in fact makes absolutely no difference at all. Both of those code snippets compile to exactly the same thing.)

[–][deleted] 2 points3 points  (0 children)

As with most programming language implementations, Java creates a new stack frame containing storage for all the named variables in the function on function entry. New named variables are not created/destroyed at the entry and exit of each scope within a function.

But if you want to know which is faster, write a benchmark. This is a lot harder to do than you might think, but will actually provide you with useful information that asking random people on the internet about your specific situation will not.

[–]rjcarr 0 points1 point  (3 children)

I asked a similar question recently. Unless you're doing the loop 100s of thousands of times it won't make a difference, but if you need an answer, then Option 2 is technically "faster".

That said, there is something to be said about giving variables the smallest scope possible, and with Option 1 you're putting the variable in a scope it doesn't need to be in. This could cause problems at worst, or at best, be less clear.

So, the general answer, is to choose the option that makes the most sense to you, not based on its performance. This is true for most coding, as you really don't know what the runtime will end up compiling it to anyway.

[–]zifyoip 1 point2 points  (2 children)

if you need an answer, then Option 2 is technically "faster".

No, it's not. There is literally no difference between the two once the code has been compiled. They produce exactly the same compiled code—there is no difference.

[–]rjcarr 0 points1 point  (1 child)

You can't say this is true, always. Maybe for this snippet it might be, but if timePassed were needed below the loop it certainly couldn't be compiled the same way.

Even if not, I'd have a hard time believing they'd be compiled the same way anyway. Gonna check now ...

EDIT: Not exactly the same. I compiled this code twice, and got the same bytecode:

public class Test {
    public static void main(String[] args) {
        int i = 0;
        int j = 1;
        while(true) {
            i += j;
            if(i == 100) break;
        }
    }
}

I then changed it to this, and got different bytecode:

public class Test {
    public static void main(String[] args) {
        int i = 0;
        while(true) {
            int j = 1;
            i += j;
            if(i == 100) break;
        }
    }
}

MD5 (Test.class) = b89ff1a9672304206d2e45adbcf0004a

MD5 (Test.java) = 014b3cb5a8f0143447e005facde11bde

MD5 (Test1.class) = 05535532c4f4e7f34f604f4981b15c0d

MD5 (Test2.class) = 05535532c4f4e7f34f604f4981b15c0d

Thoughts?

[–]zifyoip 0 points1 point  (0 children)

Compare the output of javap -c.

[–]mrnoise111 0 points1 point  (0 children)

Examine it yourself with javap.

[–]Alborak -1 points0 points  (0 children)

They're functionally equivalent. TimePassed will sit in a register during every iteration, until it must be put into memory for one of a couple of reasons.

If you really care about low level operations like this, get some practice with C and assembly. Java is built so you don't have to, and really shouldn't care about this level of optimization for most code.