- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello to all,
following question: Because of calculating speed, I would like to know the following. When using the .or.-expression in for example an if-else-statement. How is it internally parsed? Does Fortran first check the first expression and, if it is true, it skips checking for the second? Or does it check both expressions in any case?
Example
if ( (input .eq. 1.0) .or. (input .eq. 2) ) then
do something
end if
-> does Fortran check the frist expression (input=1?) and, if true skips checking the second (input=2?), or does it check both wether the first one is true or not?
I appreciate every answer, thanks a lot,
Andre
following question: Because of calculating speed, I would like to know the following. When using the .or.-expression in for example an if-else-statement. How is it internally parsed? Does Fortran first check the first expression and, if it is true, it skips checking for the second? Or does it check both expressions in any case?
Example
if ( (input .eq. 1.0) .or. (input .eq. 2) ) then
do something
end if
-> does Fortran check the frist expression (input=1?) and, if true skips checking the second (input=2?), or does it check both wether the first one is true or not?
I appreciate every answer, thanks a lot,
Andre
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The compiler may evaluate either or both subexpressions, in either order, as it sees fit. It might even not evaluate either if it can determine what the result is at compile-time. If you want to force the order of evaluation, or do 'short-circuit' evaluation, use nested IF-THEN.
Steve
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
According to the CVF Language Reference Manual, Section 4.1.4, "You should not write logical expressions whose results might depend on the evaluation order of subexpressions. The compiler is free to evaluate subexpressions in any order."
Mike
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the fast answer...
I was just too much programming Perl in the last time -> in Perl the order is defined by the programmed order ;-).
that was very helpful.
@Steve: I wanted to circumvent using nested if then, since comparisons like "if"-conditions and loops are the most computing power using elements (in general - afaik), so I wanted to reduce necessary comparisons. Anyhow, I do know something more - thanks a lot!
Andre
I was just too much programming Perl in the last time -> in Perl the order is defined by the programmed order ;-).
that was very helpful.
@Steve: I wanted to circumvent using nested if then, since comparisons like "if"-conditions and loops are the most computing power using elements (in general - afaik), so I wanted to reduce necessary comparisons. Anyhow, I do know something more - thanks a lot!
Andre
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There really isn't any difference between nested IFs and compound expression IFs. If anything, the compiler has a better chance of optimizing well with nested IFs.
You might want to see if the SELECT CASE statement is appropriate to your application - if the case values are from a small set of integers, the compiler can generate special optimized code for the selection.
Steve
You might want to see if the SELECT CASE statement is appropriate to your application - if the case values are from a small set of integers, the compiler can generate special optimized code for the selection.
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Steve, I'll keep that in mind and check it out. Is this special optimized code also usable for non-equal cases with ocmparisons to real numbers? Like greater than, lower than? I would have to compare some values to be smaller than 1.00001 and bigger than -0.00001. Normally it would be ok to use 0 and 1, but I have to deal with rounding errors ;-).
Thanks a lot, greetz form germany,
Andre
Thanks a lot, greetz form germany,
Andre
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If that's what you're doing, then SELECT CASE isn't for you. When comparing reals, you probably want something like this:
Steve
if (abs(x-1.0) < 0.00001) then ...
Steve
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page