What is faster: many ifs, or else if?

后端 未结 11 1801
夕颜
夕颜 2020-12-29 22:35

I\'m iterating through an array and sorting it by values into days of the week.

In order to do it I\'m using many if statements. Does it make any differ

11条回答
  •  萌比男神i
    2020-12-29 23:30

    I made a benchmark if there's a true difference between successive if() and if() then a few elseif()

    I put a big string and did about 20 strpos() each time (x100 000) with the two methods and it showed this result :

    Try 1 : 0.5094 (including elseif)
    Try 2 : 0.6700 (including only if)
    

    There's no doubt. I already knew sucessive elseif() were faster, even though there's a return in the middle ; it's still good to put some statistics in the answer.

提交回复
热议问题