Is the ++ operator faster than the +=?

C and its derivatives always boast about the speed increases when using the shortcut assignments like "+=". So I set up some tests to see how much faster the one is compared to the other. Here are the sources:

   class test1
{
   public static void main (String args[])
     {
	int teller = 0;
	int count = 0;
	while (true)
	  {
	     teller++;
	     count++;
	     if (teller == 100000000) break;
	  }
	System.out.println ("Done.");
     }
}


class test2
{
   public static void main (String args[])
     {
	int teller = 0;
	int count = 0;
	while (true)
	  {
	     teller = teller + 1;
	     count = count + 1;
	     if (teller == 100000000) break;
	  }
	System.out.println ("Done.");
     }
}


class test3
{
   public static void main (String args[])
     {
	int teller = 0;
	int count = 0;
	while (true)
	  {
	     teller += 1;
	     count += 1;
	     if (teller == 100000000) break;
	  }
	System.out.println ("Done.");
     }
}
   
As you can see, we do 100000000 (that is 100.000.000 or a hundred million) operations per test. This is on a 3 GHz Pentium IV system... Which is already 'slow' by all standards... Here are the results
jan@Beryllium:~/develop/java$ time java test1
Done.

real    0m0.208s
user    0m0.184s
sys     0m0.004s
jan@Beryllium:~/develop/java$ time java test2
Done.

real    0m0.206s
user    0m0.164s
sys     0m0.024s
jan@Beryllium:~/develop/java$ time java test3
Done.

real    0m0.210s
user    0m0.180s
sys     0m0.008s
jan@Beryllium:~/develop/java$
   
If there are differences, they're small. An economist would say: Yeah, man, GREAT differences! Go for the method from test2 (which, coincidentally, is the traditional non-C method). But to be honest: there's no difference between the three methods. Below is a small table to show it.
Test Method Milliseconds
1 teller++ 208
2 teller = teller + 1 206
3 teller += 1 210

As you can see: at least in Java there is no reason to use the cryptical instructions in favor of the traditional instruction from test2. You make your sources harder to read and it is easier to make an error with the ++ and the += operators.


Errorprone programming

Consider the following line of code:

   a = a + b;
   
and you want to make it 'faster' with the "+=" operator:
   a += b;
   
C programmers like to show off with this kind of instruction. See how smart I am? I can use a silly method to increment a variable. So up to he next example:
   a = b + a;
   
which essentially is the same as our initial source. In the first example we shortcut the source by So see what we get now:
   a += a;
   
which of course is a completely other statement. Now we double the 'a' variable. Apparently it is not enough to just delete the first operand behind the assignment operator. You need to keap your head with it. Analyze the intentional statement, then determine which shortcut version to use by eliminating an operand.
Of course this never happened to you (or me), but it could as well have happened to your neighbour. It could have to mine.. :o)


Page created on 31 May 2010 and

Page equipped with FroogleBuster technology