dongyakui8675 2018-03-27 12:15
浏览 32
已采纳

冗余if语句

I'm wondering if I can speed my process up, and this is the only area where I'm thinking it might be taking longer than necessary.

Basically, I'm running different select statements in db2 and mysql. I load those results in arrays to prepare them for comparison. Then I'm taking the count arrays and using them as conditions in my if statements.

The problem is I have 3 sections of IF/ELSE and the first 2 do the same exact thing (performing the same exact insert statement, just based on 2 different conditions).

$count3 holds records that exist in the table and have expired
$count4 holds records that exist in the table and have not expired

So if they're both empty, that means records don't exist so I insert. If $count3 is not empty, that means those records have expired so I insert also. If $count4 is not empty, that means records haven't expired so I update. I'm wondering if the redundancy if these first two IF/ELSE blocks are making it take longer than it could.

Here's the code:

if(empty($count3) && empty($count4)){

    //execute query 1
}
elseif(!empty($count3)){

    //execute query 1
}
elseif(!empty($count4)){

    //execute query 2
}

Is there a better way to say something like:

if (count3 and count4 are BOTH empty) OR if(count3 is not empty){
     insert
}elseif(count 4 is not empty){
    updated
}
  • 写回答

4条回答 默认 最新

  • douxin2002 2018-03-27 12:19
    关注

    Truth table:

    c3      c4      query
    -------------------
    empty   empty   1
    !empty  empty   1
    empty   !empty  2
    !empty  !empty  1   
    

    So, simplified:

    if (empty($count3) && !empty($count4)) {
        // query 2
    } else {
        // query 1
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法