File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes performance wise which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "performance wise which is better?" Watch "performance wise which is better?" New topic

performance wise which is better?

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
This is a simple doubt...
I am using If---else conditional statement in my application..
In this reg. always the statements in else lock will be execute.. it means the condtion is always false..
So, my question is... should i change the logic for executing the statements inside IF block..
according to optimization technique I am thinking that alwasy we must reduce the parsing of program code.

First it checks the condition and if it is false then it skips the If and control goes to else part..otherwise if the condition is true the if block executes.
but if the condition is 99% false it must goto else part.. skipping the if block of here automatically consumes some time for skipping the the code.. (I think so. :roll: )...

Am I correct???
if not then tel me... ur views???


SCJP 1.4
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Originally posted by saikrishna cinux:
but if the condition is 99% false it must goto else part.. skipping the if block of here automatically consumes some time for skipping the the code..

I'm not sure that necessarily follows. I can't say absolutely because I'm not familiar enough with Java bytecode or the internal workings of the JVM. But suffice to say that at runtime your code isn't parsed as it appears in the source code. The compiler has already removed all the whitespace and human-readable words and converted them into bytecode. So it shouldn't need to skip over statement1, statement2, statement3 etc... in order to get to the else block - instead, it simply skips.

For example, in pseudo-machine/Assembly code we might have:

If you follow through the logic, you'll find that if the condition is true, we go from line 1 to line 3 and execute the if(...) block, then jump to line 10. If the condition is false, we go to line 2, then jump to line 7 then execute the else block.

In either case we have 1 evaluation of the condition, 1 line jump and 3 statement executions, so they take equal processing time. In fact, if anything, my code above makes the if(...) block slower to evaluate because of the explicit GOTO 10 command!

I think in practise you will find there is extremely little, if any, difference between your two suggestions. In general, there is a much greater overhead associated with the JVM and the operating system, and this makes even one extra processor instruction irrelevant. If such a distinction really is critical to your application, then perhaps Java isn't the best choice for a language - but most of the time I prefer to trade off the performance of Assembly/machine for the ease of programming and flexibility of Java!

NOTE: I used pseudo-Assembly; in practise GOTO is often replaced by JMP (jump) and the if block would be a "branch if" which would depend on the boolean state of a register (command often given as J* where * is the name of the register holding the value to test).

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I agree with Charles that in 99.999999% of all cases, you wouldn't notice any difference. And for the rest, it really would finally depend on what the JVM does with the byte code - does it get compiled by the hot spot engine, and if so, to what instructions. So what will happen will not only depend on the version of the JVM, the underlying processor architecture and perhaps operating system, but even on how the java program gets used, how often the code already got executed etc. pp.

I'd therefore simply say: don't care about it, it's just wasted brain cells...

Anyway, moving this to our performance forum.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: performance wise which is better?
It's not a secret anymore!