优化一大组 IF 语句
我有一大组嵌套 IF 语句,我想知道是否有人对如何优化速度、大小和可读性有任何建议。
下面是一个 if 语句及其嵌套语句的示例。文件中大约有 25-30 个这样的内容。
if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
if( $row["inlet_high_critical"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
} else {
if( $row["inlet_high_warning"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
}
} else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
if( $row["inlet_low_critical"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
} else {
if( $row["inlet_low_warning"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
}
}
这个想法是;我有一个读数(温度/速度/湿度),我需要检查它是否达到任何限制(高警告/高临界/低警告/低临界),如果是,我首先需要检查我是否已经为此发出警报。如果没有发送警报,我需要检查用户是否请求了警报通知(手机/电子邮件/两者),
目前这是有效的。我只是不喜欢它有多重?我可以在这方面改进吗?
谢谢。
I have a large group of nested IF statements and I was wondering If anyone had any suggestions on how to optimize for speed, size, and readability.
Below is a sample of ONE of the if statements and its' nested statements. There will be approximately 25-30 of these in the document.
if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
if( $row["inlet_high_critical"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
} else {
if( $row["inlet_high_warning"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
}
} else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
if( $row["inlet_low_critical"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
} else {
if( $row["inlet_low_warning"] == 0 ) {
if( $row["email_notification"] == 1 ) {
}
if( $row["mobile_notification"] == 1 ) {
}
}
}
}
The idea is; I have a reading (temp/speed/moisture) and I need to check if it hits any of the limits (high warning / high critical / low warning / low critical), if it does I first need to check if I have already sent an alarm for that. If no alarm has been sent I then need to check if the user has requested alarm notification (mobile/email/both)
Currently this works. I Just don't like how heavy it is? Can I improve on this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这似乎更清楚,尽管你可以组合嵌套的 if,但我更喜欢这个
this seems to me much more clear, even though you could combine the nested ifs I'd rather prefer this one
过早优化是万恶之源 - 对于我们在这里处理的内容,无论您做什么这样做不会对性能产生太大/任何明显的影响。
话虽如此,大量
if
语句通常可以替换为一个或多个switch
结构,尽管这是否会提高性能或提高可读性尚不清楚有争议的。您还可以为重复的代码创建一些函数,尽管这实际上可能会对性能产生负面影响。根据您上面的评论...创建具有更好名称的变量对性能的影响几乎为零。如果会稍微增加您的内存使用量,但处理时间的影响将接近于零。而且,如果您将值评估为布尔值,则无需将它们显式转换为布尔值,因为 1 仍评估为
TRUE
,0 仍评估为FALSE
。但是,如果您确实想要这样做...不必要的冗长,您可以执行以下操作:
...或...
...并且它将具有相同的效果。
Premature optimisation is the root of all evil - with what we are dealing with here, no matter what you do it won't have much/any noticable impact on performance.
Having said that, a large amount of
if
statements can often be replaced with one or moreswitch
structures, although whether this improves performance or readability is debatable. You may also be able to create some functions for repeated bits of code, although this may actually negatively impact performance.From your comment above... creating variables with nicer names will have pretty much zero impact on performance. If will slightly increase your memory usage, but the processing time impact will be next to zero. And, if you are evaluating the values as booleans, you don't need to convert them to booleans explicitly, because 1 still evaluates to
TRUE
, and 0 toFALSE
. However, if you do want to do it...is unnecessarily long winded, you can do either this:
...or...
...and it will have the same effect.