View Full Version : If..then..elseif...then.. Endif ???
vinu
2nd November 2006, 08:30 AM
Hi members,
I have a question.
Is there any computational speed difference
between using two 'IF' statements seperately
and using one 'IF ELSEIF' statement together?
eg:
Type-1
if (a .eq. b) then
(do something)
endif
if (a .eq. c) then
(do something)
endif
Type-2
if (a .eq. b) then
(do something)
elseif (a .eq. c) then
(do something)
endif
Are these type-1 and type-2 equally fast ?
Please say an answer if you know,
-Vinu
rocksea
2nd November 2006, 09:42 AM
Both the functions are not same (i.e. you may not get same results!)
If a=b=c (a possible case):
In type 1, both steps are processed
In type 2, only the first step is processed.
If we consider such a possibility, checking the speed is logically incorrect.
---------------------------------------------------------------------
Then, still if we want to check the speed, there is a possibility that type 2 is faster.
Let me explain how:
In most of the softwares, the value/expression for a variable is estimated only when it is called.
(For example you must be knowing that in Ferret, an expression is processed only if the
variable is called).
Suppose your a, b & c are results of some mathematical expressions/formulas.
In type 1, values of a, b & c are calculated in all cases as all the 3 variables are checked for equivalence.
In type 2, value of c may not be calculated in several cases.
Hence in type 1 you end up with more calculations making it slower than type 1.
vinu
3rd November 2006, 12:09 PM
Good logic.
I do agree the type-1 and type-2 are different.
type-1 is safe but consumes time and awkard,
if your argument on speed is correct.
There are many places "if" statement occurs
in our code and I was thinking to replace it with
"if elseif then", for cost effect computation.
But now i think, it should be done with caution.
Thanks for your help
-Vinu
praveen
7th November 2006, 05:27 AM
vinu,roxy
yea. u are right. This one is a problem for the present models. Most of the model codes are not well structured for faster performance.
best wishes for your effort. :thumbsup:
rabin_xu
26th January 2007, 09:04 AM
I think the first is faster than the second
Copyright ©20005 Oceanographers Net