Question about Java operators

I learned how to program in Pascal.

In Java I have two options:

1. a = a+1

2. a++

My Java teacher claims that option 1 makes the program slower. I would like to use Option 1 because this is the same as in Turbo Pascal.

If wat she's teaching us is not true, please let me know as soon as possible.

I would like a lot of confirmations if it's not true, so that I can show it to her...

[427 byte] By [JelleMeesa] at [2007-11-27 6:02:14]
# 1

I suppose it's possible that the first one might be slower, but I defy your teacher to create a realistic program where that's noticeable. Don't worry about those little micro-optimizations.

Still, you should prefer the second one because it's more concise and it's the standard idiom for incrementing a value.

Also, the two are not equivalent. The value of the expression in the first case is the new value of a, and in the second case it's the original value of a.

jverda at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...
# 2
As jverd said, this is a micro-optimization. They are different at the byte code level but not performance wise by any useful benchmark.
YoGeea at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...
# 3
> I learned how to program in Pascal.Sorry, suicide is your only option :-)
corlettka at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...
# 4

> As jverd said, this is a micro-optimization. They are

> different at the byte code level but not performance

> wise by any useful benchmark.

Absolutely correct (as expected from jverd and Yogee).

Disassembling the class:

public class Adding {

public static void main(String[] args) {

int a = 0, b = 0;

a = a + 1;

b++;

}

}

shows the bytecodes to be different:

kev@mymachine ~/java$ javap -c Adding

Compiled from "Adding.java"

public class Adding extends java.lang.Object{

public Adding();

Code:

0:aload_0

1:invokespecial#1; //Method java/lang/Object."<init>":()V

4:return

public static void main(java.lang.String[]);

Code:

0:iconst_0

1:istore_1

2:iconst_0

3:istore_2

4:iload_1

5:iconst_1

6:iadd

7:istore_1

8:iinc2, 1

11: return

}

only slightly.

I ran this program three times:

public class OptTest {

public static void main(String[] args) {

int a=0, b=0;

long astart = System.currentTimeMillis();

for (long i = 0; i < 10000000000; i++) {

a = a + 1;

}

long aend = System.currentTimeMillis();

System.out.println("Ten billion 'a=a+1' statements took " + (aend-astart) + " mil.");

long bstart = System.currentTimeMillis();

for (long i = 0; i < 10000000000; i++) {

b++;

}

long bend = System.currentTimeMillis();

System.out.println("Ten billion 'b++' statements took " + (bend-bstart) + " mil.");

}

}

and got completely inconsistent results:

kev@mymachine ~/java$ java OptTest

Ten billion 'a=a+1' statements took 43984 mil.

Ten billion 'b++' statements took 42640 mil.

kev@mymachine ~/java$ java OptTest

Ten billion 'a=a+1' statements took 43467 mil.

Ten billion 'b++' statements took 43749 mil.

kev@mymachine ~/java$ java OptTest

Ten billion 'a=a+1' statements took 42925 mil.

Ten billion 'b++' statements took 43362 mil.

So, different? Yes. Noticeably? No. And the first option doesn't even slow down your program.

<disclaimer>This is not a scientific experiment. This offer not available in Guam or where explicitly illegal. No puppies, dolphins, or baby seals were hurt in the making of this post.</disclaimer>

kevjavaa at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...
# 5
> In Java I have two options:> 1. a = a+1> 2. a++3. a += 1;
floundera at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...
# 6

> > In Java I have two options:

> > 1. a = a+1

> > 2. a++

>

> 3. a += 1;

heretic, and you're forgetting the core of procedural programming: anything done more than once must be a method.

So:

int add(int i, int j) {

return i + j;

}

...

a = add(a, 1);

jwentinga at 2007-7-12 16:43:22 > top of Java-index,Java Essentials,New To Java...