In theory yes these would be the same.

Option 2 is the prefered approach, not least due do only having
one exit point from the function, but also it is far easier for the
compiler to
optimize.

In .net a switch case should be faster again.
(this doesn't apply to all languages/compilers, .net optimizes switch
particularly well)

finally, modern processers have a facility to predict which
conditional branch it will execute
before getting there, this facility kicks in for specific assembly
'patterns', which compilers
try to emit.

Using the language feature designed for the purpose makes it easier on
the compiler
because it knows what you're trying to do, and has the optimizations
prebuilt,
with other constructs the compiler has to figure out what you are
trying to do before attempting
to optimize...

In .Net you can use Reflector to view the IL (similar to assembly)
returned from these constructs
to see how much they differ.

Finally, the best thing to do is see for yourself. Write a program
that loops around each option
a million or so iterations, and time it.... see which comes out
fastest...



On Jan 9, 9:09 am, santhosh vs <[email protected]> wrote:
> Consider the two samples.
> *1.*
> void/object function()
> {
> if(cond1)
> {
>   ---
>   return;}
>
> if(cond2)
> {
>   ---
>   return;}
>
> if(cond3)
> {
>   ---
>   return;
>
> }
> return;
> }
>
> *2.*
> void/object function()
> {
> if(cond1)
> {
>   ---}
>
> else if(cond2)
> {
>   ---}
>
> else if(cond3)
> {
>   ---
>
> }
> return;
> }
>
> I have doubt in mind weather this two make a difference in call stack etc..
> Or the performance and work load is same for both flow?
>
> --
>
> Santhosh Vhttp://techplex.wordpress.com

Reply via email to