This discussion is archived
7 Replies Latest reply: May 7, 2009 1:43 PM by 807588 RSS

Simple question about Memory Usage and File Size

807588 Newbie
Currently Being Moderated
Hello all!

I recently (5 minutes ago) joined SDN to ask this question, but now that I have an account I imagine I'll be active here.

Please consider the following two snippits of code... Obviously they are two methods of doing the same thing.
count += 1;
and
count = count + 1;
I understand that maybe the advantage of the first is fewer characters to type, and that the advantage of the latter is that the code is probably more human readable. But I have a deeper question...

Once these snippits are compiled into bytecode, would there be any difference in performance? Would one be larger than the other? Would one take a bit longer to run? or does the Java Compiler have a single string of code that both of the above options and compiled into?

I know that a single instance of this wouldn't make a bit of difference on modern computers, but as the snippits will most likely be used in a loop, a loop running several minutes or longer might show performance differences.

Thanks for helping!

Daniel

P. S. I know I'm flagged as a student - this is not a test question :) Just the question of an inquisitive beginner java student.
  • 1. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    An add operation is an add operation is an add operation.
  • 2. Re: Simple question about Memory Usage and File Size
    abillconsl Explorer
    Currently Being Moderated
    You can look at the byte code yourself if you want. There you "should" be able to see how the code is translated.

    You can also run a test program in a huge loop and check the time of each.

    But the important answer, is as given - don't worry about it. Code whichever way is more to your liking. Readable code is more important than any "possible" tiny performance improvements.
  • 3. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    They boil down to the same bytecode.

    Important Safety Tip: worrying about bytecode-level performance at your skill level is almost certainly a waste of time, so don't. Strive to write clean, easy-to-read, maintainable code.
  • 4. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    Thanks for the quick replies!

    I am grateful for the concise responses and advice for continuing learning.

    es5f2000 - I'll be sure not to worry too much about it :) Mostly I was just thinking about how often there are many ways to write the same code, and there is always a most efficient way.

    Efficiency shouldn't be traded for readability, usually, unless there is a marked decrease... then I suppose one could consider such the option. I agree.

    I was just curious. Thanks again!
  • 5. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    This is something you could have fun trying by yourself by trying both and looking at the difference between the class files.

    That's what I did, and I found this:
    public class PlusEqualsTest {
       public static void main(String... args) {
          int count = 0;
          count += 1;
       }
    }
    Compiled to this: (viewed through Eclipse's class file viewer)
     public static void main(java.lang.String... arg0);
        0  iconst_0
        1  istore_1
        2  iinc 1 1
        5  return
          Line numbers:
            [pc: 0, line: 3]
            [pc: 2, line: 4]
            [pc: 5, line: 5]
    }
    While this:
    public class PlusEqualsTest {
       public static void main(String... args) {
          int count = 0;
          count = count + 1;
       }
    }
    Compiled to this:
      public static void main(java.lang.String... arg0);
        0  iconst_0
        1  istore_1
        2  iload_1
        3  iconst_1
        4  iadd
        5  istore_1
        6  return
          Line numbers:
            [pc: 0, line: 3]
            [pc: 2, line: 4]
            [pc: 6, line: 5]
    }
    Now how much of a performance difference that makes, I don't know. Presumably some, otherwise why would the compiler overcomplicate things?

    I saw the byte code "iinc 1 1" (increase by one) and figured that it was a special case (where the compiler recognized the right-hand side was a literal and so treated it as a literal instead of an expression), so I also tried compiling this:
    public class PlusEqualsTest {
       private static int rhs = 1;
       public static void main(String... args) {
          int count = 0;
          count += rhs;
       }
    }
    and got this:
      public static void main(java.lang.String... arg0);
        0  iconst_0
        1  istore_1
        2  iload_1
        3  getstatic PlusEqualsTest.rhs : int [2]
        6  iadd
        7  istore_1
        8  return
          Line numbers:
            [pc: 0, line: 4]
            [pc: 2, line: 5]
            [pc: 8, line: 6]
    And then did the same with the second:
    public class PlusEqualsTest {
       private static int rhs = 1;
       public static void main(String... args) {
          int count = 0;
          count = count + rhs;
       }
    }
    and got this:
      public static void main(java.lang.String... arg0);
        0  iconst_0
        1  istore_1
        2  iload_1
        3  getstatic PlusEqualsTest.rhs : int [2]
        6  iadd
        7  istore_1
        8  return
          Line numbers:
            [pc: 0, line: 4]
            [pc: 2, line: 5]
            [pc: 8, line: 6]
    They're identical! In other words, it seems like when the right-hand side is a constant or literal, the compiler treats the first differently than the second, but when it's a variable expression, they compile to identical bytecode.

    In further other words, it's not just syntactic sugar. There is a difference in the compiled code. But the others are right, it's not something you should be concerned about.

    Edited by: endasil on 7-May-2009 4:39 PM
  • 6. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    Yes! Exactly what I was hoping for!

    I have never viewed compiled code and I didn't know eclipse could do it.

    Your answer is perfect! I was thinking of doing similar tests, but with notepad class files don't open so prettily...

    A very interesting conclusion as well. Something I won't forget, but won't fret over either (as previously mentioned).

    Dukes to endasil for the effort and the concise explanation!

    Daniel
  • 7. Re: Simple question about Memory Usage and File Size
    807588 Newbie
    Currently Being Moderated
    count++; is even sorter still.

    The only difference between count += n; and count = count + n; is that n is implictly cast to the type of count, int the first example, but not the second.
    int count = 1;
    count += 5.5; // compiles
    count = count + 5.5; // does not compile.
    count = (int) (count + 5.5); // compiles
    This looks odd but consider this example
    byte b = 1;
    b += 1; // compiles
    b = b + 1; // does not compile as b + 1 is of int type.
    b = (byte) (b + 1); // compiles.