if (any(foo == (/1,2,3,4/)) 而不是 if ((foo == 1).or.(foo == 2).or. ...?
我有很多 if 语句,内容类似于
if ((foo == 1).or.(foo == 2).or.(foo == 3).or.(foo == 4)) then
call bar1
end if
if ((foo == 3).or.(foo == 4).or.(foo == 5).or.(foo == 6)) then
call bar2
end if
是否有任何理由(速度、风格等)不使用这样的东西?
if (any(foo == (/1,2,3,4/))) call bar1
if (any(foo == (/3,4,5,6/))) call bar2
在我的真实示例中,数字不一定是连续的。所以我不能
if ((foo >= 1).and.(foo <=4)) call bar1
if ((foo >= 3).and.(foo <=6)) call bar1
故意使用两个if条件的重叠,这使得我理解不可能使用SELECT CASE语句。
I have numerous if statements that read something like
if ((foo == 1).or.(foo == 2).or.(foo == 3).or.(foo == 4)) then
call bar1
end if
if ((foo == 3).or.(foo == 4).or.(foo == 5).or.(foo == 6)) then
call bar2
end if
Are there any reasons (w.r.t. speed, style, etc.) to not use something like this?
if (any(foo == (/1,2,3,4/))) call bar1
if (any(foo == (/3,4,5,6/))) call bar2
In my real example the numbers are not necessarily sequentially. So I can not use
if ((foo >= 1).and.(foo <=4)) call bar1
if ((foo >= 3).and.(foo <=6)) call bar1
the overlap of the two if conditions is deliberate, which makes it in my understanding impossible to use the SELECT CASE statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
高性能马克已经回答了您的大部分问题,但请允许我提出一个很好的替代方法。
Fortran 允许运算符重载,这允许我们重载
.in。
查看某个项目是否在列表中。这个代码看起来像然后你的代码就变成了
在我看来这是最干净的方法,并且它应该具有与其他方法类似的性能。您可以根据需要重载
.in.
以采用其他类型(包括用户定义的类型)。High Performance Mark has answered most of your question, but allow me to present a nice alternative method.
Fortran allows operator overloading, and this allows us to overload
.in.
to see if an item is in a list. The code for this would look likeAnd then your code simply becomes
In my opinion this is the cleanest way of doing this, and it should have similar performance to other methods. You can overload
.in.
to take other types (including user-defined types) as needed.关于问题的速度部分 - 如果这是您关心的问题,那么请进行测试和测量。如果您认为不值得花时间进行测试和测量,那么 (a) 您可能是对的,我认为我曾经开发过一个重要的 Fortran 程序,其中两种方法之间的速度差异可能是物质利益; (b) 这也不值得我花时间!
至于风格 - 问题的这一部分在这里是偏离主题的 - 但我并不觉得这一切令人反感,尽管我可能会编写一个带有签名
memberQ(foo, list)
的小函数和明显的实施。With regards to the speed part of the question - if this is a concern for you then test and measure. If you don't think it's worth your time testing and measuring then (a) you're probably right, I don't think that I have ever worked on a significant Fortran program where the speed differences between the two approaches could have been of material interest; and (b) it's not worth my time either!
As to the style - that part of the question is off-topic here - but I don't find it all objectionable, though I might write a wee function with signature
memberQ(foo, list)
and the obvious implementation.