performance wise which is better?

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. If { statement1 statement2 statement3 } else { statementx statementy statementz }

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 statements..so here automatically consumes some time for skipping the the code.. (I think so. :roll: )...

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

:roll:

A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4

Originally posted by saikrishna cinux: but if the condition is 99% false it must goto else part.. skipping the if block of statements..so 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).

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.

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