2011-09-12 59 views
0

我有一大堆嵌套的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) { 

      } 
     } 
    } 
} 

这个想法是;我有一个读数(温度/速度/湿度),我需要检查它是否遇到任何限制(高警告/高危/低警告/低危),如果它首先需要检查我是否有已经为此发出了警报。如果没有发出警报,则需要检查用户是否要求警报通知(手机/电子邮件/两者)

目前这项工作。我只是不喜欢它有多沉重?我可以改进吗?

谢谢。

+0

的第一件事就是创建较短的变量名,如'$ inlet_moisture = $ row [“inlet_moisture”]'等等。在这个过程中,我也会转换为'boolean',以避免在每个地方明确检查'== 1'。 –

+0

@Oli,这会对性能产生影响吗?此外,这些值以布尔值的形式存储在我的数据库中,但是MySQL为布尔值返回0和1。我应该''$ email_notification = $ row [“email_notification”] == 1? true:false;' – rlemon

+2

我怀疑这对性能几乎没有影响(尽管你应该对它进行配置以确认)。另外,你不需要'? true:false'。 –

回答

2

这更清楚在我看来,即使你可以结合嵌套IFS我宁愿喜欢,我会做,以提高可读性这一

if($row["inlet_moisture"] > $row["inlet_moisture_high_critical"]) { 
    if($row["inlet_high_critical"] == 0) { 
    $message = 'the pertinent message'; 
    } 
} 
else if($row["inlet_moisture"] > $row["inlet_moisture_high_warning"]) { 
    if($row["inlet_high_warning"] == 0) { 
    $message = 'the pertinent message'; 
    } 
} 
else if($row["inlet_moisture"] < $row["inlet_moisture_low_critical"]) { 
    if($row["inlet_low_critical"] == 0) { 
    $message = 'the pertinent message'; 
    } 
} 
else if($row["inlet_moisture"] < $row["inlet_moisture_low_warning"]) { 
    if($row["inlet_low_warning"] == 0) { 
    $message = 'the pertinent message'; 
    } 
} 


if($row["email_notification"] == 1) { 
    sendMessage($message, $email); 
} 
if($row["mobile_notification"] == 1) { 
    sendMessage($message, $mobile);  
} 
+0

您可以在开始时将initializ $ message初始化为false或null,以便稍后在通知条件下将其用作标志。例如:if($ row [“email_notification”] && $ message){...} –

1

Premature optimisation is the root of all evil - 随着我们在这里处理的事情,无论你做什么,它都不会对性能产生太多/任何明显的影响。

话虽如此,大量的if陈述通常可以与一个或多个switch结构所取代,这是否虽然提高性能可读性是值得商榷的。您也可以为重复代码创建一些函数,但这可能实际上会对性能产生负面影响。

从上面的评论中...创建具有更好名称的变量对性能几乎没有影响。如果稍微增加内存使用量,但处理时间影响将接近于零。而且,如果您将值作为布尔值计算,则不需要明确将它们转换为布尔值,因为1仍然计算为TRUE,0和FALSE。但是,如果你想做到这一点

$email_notification = $row["email_notification"] == 1 ? true : false; 

...不必要地长篇大论,您可以执行此:

$email_notification = $row["email_notification"] == 1; 

......或者......

$email_notification = (bool) $row["email_notification"]; 

...它会有同样的效果。

+0

我最大的'优化'点将是可能的;与上述要求/结构,使这个任何更小或紧凑,但仍然处理所有情况。但我喜欢你的观点。 – rlemon

+0

@rlemon从上面的代码中,如果您对文档的大小感到困扰,可以将其缩减为单个块和“foreach”,但这可能会降低性能。我会编辑我的答案,向你展示我的意思...... – DaveRandom

+0

@rlemon菲达基拉的回答可能是你以后的想法。没有更多的处理器/内存高效,但肯定会提高可读性。 – DaveRandom