-3

So lately I've been trying to figure out how 'heavy' some statements are. For example is it a good thing to check for every possible exception with if statements or will that slow down the programm considerably? So I bassically want to know how 'heavy' the folling statements get when used a lot. This is mostly just a question out of curiosity because today's computer are so fast that it probably doesn't matter But it will also help me choose between different ways of doing things though perfomance improvements might only be minimal.

A simple while loop how 'heavy' is the loop itself not the code inside it?

while(true){}

A for loop probably smiliar to the while loop?

for(int i = 0; true; i++){}

A do while loop probably smiliar to the above two too?

do{...}while(true)

An if statement how 'heavy' does this get?

if(true){}

A switch statement

switch(0){
   case 0:
}

And is a switch statement less 'heavy' or 'heavier' than else if statements

And the instanceof check how 'heavy' is it?

if(obj instanceof Player){}

An is null check I heard that this one is really 'light' is that true?

if(obj == null){}

And a constructor call

new Object();

And a method call

MyClass.doSomething();

A variable assignment

int i = 10;

I tried searching on the internet but I didn't find a web page that was comparing them all to each other or something smiliar. But if some of you have a good documentation on this I would be more than happy to read it.

Roan
  • 1,150
  • 2
  • 20
  • 29
  • 3
    how "heavy" is infinite while loop? What the hell, man. – MightyPork Jul 07 '14 at 20:15
  • well that's just an example I don't mean it to be infinite there could be a `break` inside it – Roan Jul 07 '14 at 20:16
  • Well... you can try to benchmark it.. though I dont know how you'd benchmark the loop's overhead. – MightyPork Jul 07 '14 at 20:17
  • I think by "heavy" he is actually asking which of these ways is the most "light-weight" and "efficient". An answer I would like to know as well. – Tom Testicool Jul 07 '14 at 20:18
  • 3
    If you are interested in the performance of a section of code, the answer is it depends on how long it has been running i.e. how warmed up it is, what the extra code is and what data it is processing as well as how warm your CPU caches are. You can't say a piece of code is "heavier" than another without context. – Peter Lawrey Jul 07 '14 at 20:19
  • @Tom Testicool Yeah that's indeed what I am trying to find out. – Roan Jul 07 '14 at 20:19
  • @PeterLawrey I am not trying to find out how good my code is I am trying to find out how heavy the loops/ifs/calls itself are without taking the code in it into account. – Roan Jul 07 '14 at 20:20
  • The problem is that the margin for error is around 100x. On top of this many operations take so much longer than these (apart from the infinite loops) that it makes these look trivial. e.g. a `System.out.println(/*nothing*/)` can be 100000x more expensive. – Peter Lawrey Jul 07 '14 at 20:21
  • Interesting I didn't know that – Roan Jul 07 '14 at 20:21
  • @Roan without context that is impossible as the code can be optimised away, e.g. it might take no time at all in some contexts as it simply disappears. – Peter Lawrey Jul 07 '14 at 20:22
  • ok so It would be parctically inpossible to see the difference because they are excecuted faster than messurable? And if there's nothing in it it won't be excecuted. And with something in it it can't be measured... – Roan Jul 07 '14 at 20:23
  • @Roan you can measure them, but the results you will get depends on how they are used. i.e. they will tell you nothing about how it will behave with different data, in a different program, on a different machine, on a different version of Java. – Peter Lawrey Jul 07 '14 at 20:24
  • True but if you do that you are putting code inside the statements and then the time that the code takes gets counted aswell. – Roan Jul 07 '14 at 20:25

1 Answers1

1

The answer to pretty much everything there is "It depends". It depends on the JITC, what's inside those calls, surrounding code, how hot the code is, how good your branch predictor is, etc. But if you're worried about the performance of control flow structures, you're almost certainly looking in the wrong place...

Disclaimer: This analysis goes right out the window if you actually put something in the loops and/or evaluate a "real" condition, because evaluating those would dwarf the cost of the control flow structures themselves. But I'll just take everything literally now, because otherwise I can't give a solid answer.

while(true){}

This would probably be optimized to an unconditional branch by the JITC. So not "heavy" at all.

for(int i = 0; true; i++){}

Again, JITC optimizations could probably turn this into an unconditional branch + increment. "heavier" than while(true), but that's because of the increment more than anything. Probably could be optimized further by the JITC; in this case, it's possible that the increment would be skipped entirely.

do{...} while(true);

Same as while(true). Probably optimized to unconditional branch by JITC.

if(true){}

If this isn't eliminated in bytecode compilation (and it might not be; I think I remember some special rules concerning how control flow is evaluated for if statements), then it'll probably be optimized by the JITC to a no-op and essentially be eliminated from the program.

switch(0){ case 0: }

Not sure about this case specifically, but I wouldn't be surprised if the JITC optimized this away entirely. Otherwise, a switch could be a jump table or a binary-search-ish instruction, depending on how sparse the cases are.

And is a switch statement less 'heavy' or 'heavier' than else if statements

This depends entirely on 1) whether you can use one instead of the other, and 2) what you are comparing. So can't say anything about this.

if(obj instanceof Player){} if(obj == null){}

These two particular snippets would probably be removed by the JITC, otherwise I would expect that instanceof and == null would be reasonably fast, as I think there are bytecode instructions that I would expect would be reasonably optimized. I'm not exactly sure whether to call these "heavy" or "light", because there isn't a point of comparison...

new Object();

I hear object creation these days is cheap. So I'll go with "light", if you're talking about the actual allocation. The performance of the entire statement, though, depends on what's going on in the constructor.

MyClass.doSomething();

Depends on whether the method has been inlined. If so, the it's as cheap as it gets. If not, then it's the cost of a vtable lookup, which may or may not be expensive. Not sure.

int i = 10;

Assigning a value/reference probably isn't expensive at all, since that's a decent amount of a program...

awksp
  • 11,292
  • 4
  • 35
  • 44
  • Thanks a lot for your answer. That was excactly what I was looking for. – Roan Jul 07 '14 at 20:36
  • @Roan No problem! Please don't actually use this info in your code, though; as I said, it's pretty much worthless if you change any of the code snippets in your question, and as you probably noted, the answers towards the end got a lot less certain... – awksp Jul 07 '14 at 20:39
  • Yeah I won't use it as I said this question was mostly out of curiosity. But still tanks a lot :) – Roan Jul 07 '14 at 20:40