php 哪个更快更好,Switch Case 还是 if else if?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10773047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:01:02  来源:igfitidea点击:

Which is Faster and better, Switch Case or if else if?

phpperformanceif-statementswitch-statement

提问by Ballu Rocks

Which is the better and fastest methods : if or switch ?

哪个是更好和最快的方法:if 或 switch?

if(x==1){
  echo "hi";
} else if (x==2){
  echo "bye";
}

switch(x){
  case 1
    ...
  break;
  default;
} 

回答by ThiefMaster

Your first example is simply wrong. You need elseifinstead of just else.

你的第一个例子是完全错误的。您需要elseif而不仅仅是else.

If you use if..elseif...or switchis mainly a matter of preference. The performance is the same.

如果使用if..elseif...switch主要是偏好问题。性能是一样的。

However, if all your conditions are of the type x == valuewith xbeing the same in every condition, switchusually makes sense. I'd also only use switchif there are more than e.g. two conditions.

然而,如果所有条件的类型x == valuex是相同的在每一个条件,switch通常是有道理的。我也只会switch在有两个以上的条件时使用。

A case where switchactually gives you a performance advantage is if the variable part is a function call:

switch如果变量部分是函数调用,则实际上会给您带来性能优势的一种情况:

switch(some_func()) {
    case 1: ... break;
    case 2: ... break;
}

Then some_func()is only called once while with

然后some_func()只被调用一次,而

if(some_func() == 1) {}
elseif(some_func() == 2) {}

it would be called twice - including possible side-effects of the function call happening twice. However, you could always use $res = some_func();and then use $resin your ifconditions - so you can avoid this problem alltogether.

它将被调用两次 - 包括函数调用发生两次可能产生的副作用。但是,您可以始终使用$res = some_func();然后$res在您的if条件下使用- 这样您就可以完全避免这个问题。

A case where you cannotuse switch at all is when you have more complex conditions - switchonly works for x == ywith ybeing a constant value.

在那里你的情况不能在所有使用开关是当你有更复杂的条件-switch仅适用于x == yy是一个恒定值。

回答by Guest

According to phpbench.com, if/elseif is slightly faster, especially when using strict comparison (===).

根据phpbench.com, if/elseif 稍微快一点,尤其是在使用严格比较 (===) 时。

enter image description here

在此处输入图片说明

But it'll only really matter if you want to shave off microseconds on a function that'll be called thousands of times.

但是,如果您想在一个将被调用数千次的函数上减少微秒,那才是真正重要的。

回答by Alix Axel

General rule is use switchwhenever the number of conditions is greater than 3(for readability).

当条件数大于 3 时(为了可读性),使用switch一般规则。

if/ else if/ elseis more flexible (hence better), but switchis slightly faster because it just computes the condition once and then checks for the output, while ifhas to do this every time.

if/ else if/else更灵活(因此更好),但switch稍微快一些,因为它只是计算条件一次,然后检查输出,同时if必须做到这一点的每一次。

EDIT: Seems like switchis slower than ifafter all, I could swear this was not the case...

编辑:似乎switchif毕竟,我可以发誓事实并非如此......

回答by Dan Bray

When using ==, performance of if ... elseifcompared to switchis almost identically. However, when using ===, if ... elseifis about 3 times faster (according to: phpbench).

使用 时==,与if ... elseif相比的性能switch几乎相同。但是,当使用===, 时if ... elseif大约快 3 倍(根据:phpbench)。

Generally, you should go with what is most readable and use switchwhen making more than 3 comparisons. If performance is a major concern and you don't need to make any type conversions, then use if ... elseifwith ===.

通常,switch在进行 3 次以上的比较时,您应该选择最易读和最常用的内容。如果性能是一个主要问题并且您不需要进行任何类型转换,那么使用if ... elseifwith ===

回答by sczdavos

It's depending on usage. If you have fxp status (online, away, dnd, offline...) its better use switch.

这取决于使用情况。如果您有 fxp 状态(在线、离开、dnd、离线...),最好使用 switch。

switch(status)
{
case 'online':
...
}

But if you wanna something like this

但是如果你想要这样的东西

if ((last_reply.ContainsKey(name)) && (last_reply[name] < little_ago))

or

或者

if (msg.ToString()[0] == '!')

its better use if else.

否则它更好用。

回答by will

I belive the compiler will turn them into very similar, or maybe even identical code at the end of the day.

我相信编译器最终会将它们变成非常相似,甚至可能完全相同的代码。

Unless you're doing something weird, don't try and do the optimisation for the compiler.

除非你在做一些奇怪的事情,否则不要尝试为编译器做优化。

Also, developer time is generally more important than runtime (with the exception of games), so it'sbbetter to make its more readable and maintainable.

此外,开发人员时间通常比运行时更重要(游戏除外),因此最好使其更具可读性和可维护性。

回答by will

in my opinion the "if/else" is faster but not better than switch but i prefer this:

在我看来,“if/else”更快,但并不比 switch 好,但我更喜欢这个:

echo ($x==1?"hi":($x==2?"bye":""));

if you have to do 1,2 cases like if/else if/else

如果你必须做 1,2 个案例,比如 if/else if/else