-6

I am wondering which of the following is the most efficient?

int x = 1, y = 2;
System.out.print(x+y)

or...

int x = 1, y = 2, z = 3;
System.out.print(z);

I'm guessing it's the first, but not sure - thanks.

Jamie
  • 33
  • 1
  • 2
  • 8
    The difference literally doesn't matter. – Andy Turner Oct 03 '16 at 19:31
  • 3
    if you're worried about the literal nanosecond difference between those two, you're optimizing the wrong thing... – Marc B Oct 03 '16 at 19:32
  • 4
    Define "efficient". Faster execution time? Lower memory footprint? Smaller resulting bytecode? Something else? When you execute and profile these, which meets your criteria? – David Oct 03 '16 at 19:32
  • If you will have to work with the added numbers afterwards, the second one is better. – Nikolas Charalambidis Oct 03 '16 at 19:33
  • 2
    Any efficiency gains in one or the other have been offset already by the time you have taken to ask this question. – Andy Turner Oct 03 '16 at 19:33
  • 5
    If `x`, `y`, and `z` are otherwise unused, then the two could be compiled to identical bytecode, with *all* of those variables optimized out. – John Bollinger Oct 03 '16 at 19:34
  • I would also state that the mathematical operation is going to be less efficient than the static constant declaration, but the fact of the matter is that this is not where you want to optimize. Profile your code and figure out what needs to be optimized. – Makoto Oct 03 '16 at 19:34
  • 2
    The only surefire way you can answer "is X more efficient than Y" questions is to *benchmark* the two approaches. But the effort expended in doing such a nano-optimization is totally not worth it - there is no way that this is the most important bottleneck in your code. – Andy Turner Oct 03 '16 at 19:35
  • It does not matter I think. Both might be compiled to the same after optimization (in theory), in practice it depends on compiler, architecture, ... I would not worry about that to be honest. – Ely Oct 03 '16 at 19:35
  • If `z` is supposed to be the sum of `x` and `y`, then it should be defined as `x+y` for readability and maintainability. It's much more efficient when you don't have to spend time tracking a bug when you change one of the numbers and forget to update the implied sum. – JJJ Oct 03 '16 at 19:36
  • You can try. Do this 100 000 times and see the time difference by printing timestamps. – RaminS Oct 03 '16 at 19:36
  • 1
    The cost of calling `System.out.print` is at least 10,000x higher. It's like asking whether one ant or another is heavier when standing on an elephant. – Peter Lawrey Oct 03 '16 at 19:41
  • Be specific about your issue in the wording of your title. – Basil Bourque Oct 03 '16 at 20:25
  • @Jamie For the record; given the high downvote count you can consider to delete this question (although I think that the comments and answers you got have some merit); or you could go forward and accept on of the answers. In case you decide to keep the question, I can offer some "coaching" when I find new postings from you; meaning I can try to compensate the the negative reputation that you got for this question by helpful comments/work on my side for some time. But as said; the decision whether to keep/delete the question is all yours. – GhostCat Oct 04 '16 at 07:06

3 Answers3

5

The real answer is: talking about efficiency on such a level does not make any sense at all.

Keep in mind that the overall performance and efficiency of a Java program is determined by many many factors - for example when/how the JIT kicks in in order to turn byte code into machine code.

Worrying about such subtleties will not help you to create a meaningful, maintainable, "good OO" design. Heck; in your case, depending on context, it could even be that the compiler does constant folding and turns your whole thing into println(3) (as it is really straight forward to throw away those variables); so maybe in both cases, the compiler creates the exact same bytecode.

Dont get me wrong: it is fair to ask/learn/understand what compilers, JVMs and JITs do. But: dont assume that you can categorize things that easily into "A more efficient than B".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Agree and +1. To add to this, it is the compiler's job to optimize things at this level, not the programmer's. Programmer optimizations should happen at the algorithm level. – nhouser9 Oct 03 '16 at 19:38
  • I'm just starting to learn, so i'm looking for the best practices, so i value all of the answers and will consider all suggestions in relevant situations, so thanks. – Jamie Oct 03 '16 at 19:40
  • @Jamie Thats fair. And I already updated my question in that direction. – GhostCat Oct 03 '16 at 19:42
  • " the compiler creates the exact same bytecode." It doesn't, at least not for me; coincidentally, it creates the same number of bytecode instructions, though. The JIT might optimize to `println(3)`, though. – Andy Turner Oct 03 '16 at 19:42
  • @AndyTurner Please note that the sentence starts with "*maybe* in both cases". I didnt say that it must happen; but this kind of optimization is really one of the most simple things that a compiler can do. So I think it is not completely out of reason to *assume* it could happen. – GhostCat Oct 03 '16 at 19:43
  • @GhostCat "at least not for me". I'm not denying the existence of black sheep in Scotland, I'm just observing that the only one I've seen is white. But, as JonSkeet notes [here](http://stackoverflow.com/a/5981485/3788176): "javac will only do a very little optimization, if any." – Andy Turner Oct 03 '16 at 19:46
  • @AndyTurner Touche. But for the record: I have not been to Scotland yet, so I can't comment on the color of their sheep. – GhostCat Oct 03 '16 at 19:48
1

If you truly mean the case where you have supplied all the literal values like that, then the difference doesn't exist at all, at least not after your code is JIT-compiled. In either case you will have zero calculation done at runtime. The JIT compiler will work out the result and hardcode it into all its use sites. The optimization techniques involved are Constant Propagation and Constant Folding.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
-4

It would be second option as you do not need any memory for calculation. You're just print a number instead of adding them together and than printing.

This is simple example, so performance is not noticeable at this level..

Good practice is to assign the task appropriately to different functions.

Omar
  • 180
  • 2
  • 15