dprq18175 2015-07-19 11:23
浏览 60
已采纳

PHP switch语句VS if elseif语句基准

I check this PHP website benchmark for check switch statement VS if else if statement. And I see this result:

Is a there a difference between switch and if structures?. Call 1'000x

  • 141 % *if and elseif (using ==)* Total time: 165 µsview code
  • 139 % *if, elseif and else (using ==)* Total time: 162 µsview code
  • 110 % *if and elseif (using ===)* Total time: 128 µsview code
  • 100 % *if, elseif and else (using ===)* Total time: 117 µsview code
  • 149 % *switch / caseTotal* time: 174 µsview code
  • 181 % *switch / case / default* Total time: 211 µsview code

In result i see if else if is faster (+ **100 %** *if, elseif and else (using ===)* Total time: 117 µsview code).

This benchmark is true and if, elseif and else (using ===) is better and faster as switch statement?!

  • 写回答

2条回答 默认 最新

  • duanbi3786 2015-07-19 11:26
    关注

    Whether you get exactly the same results will vary based on what conditions you are evaluating, your equipment, settings, and other factors. But yes, generally if/elseif/else with strict comparison (===) will outperform switch. The reason is that switch uses "loose" (i.e., type-insensitive) comparison (==), which is slower than type-sensitive comparison (===).

    Keep in mind that these differences are extremely tiny and are going to be dwarfed by any inefficiencies in your algorithm. So, you should only tune for details like this after you are sure you have eliminated other major bottlenecks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决