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

后端 未结 8 1231
予麋鹿
予麋鹿 2020-11-28 05:52

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

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

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


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 06:12

    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.

提交回复
热议问题