In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "which is faster, switch or ifelse of PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "which is faster, switch or ifelse of PHP".
Who is faster, switch or ifelse of PHP?
In the case of multiple if conditions, it will be clearer for the code to use switch instead of ifelse. What about their efficiency comparison? From the PHP manual, I found that someone had already compared it, and I also experimented with his code:
$s = time ()
For ($I = 0; $I < 10000000000; + + $I) {
$x = $I% 10
If ($x = = 1) {
$y = $x * 1
} elseif ($x = = 2) {
$y = $x * 2
} elseif ($x = = 3) {
$y = $x * 3
} elseif ($x = = 4) {
$y = $x * 4
} elseif ($x = = 5) {
$y = $x * 5
} elseif ($x = = 6) {
$y = $x * 6
} elseif ($x = = 7) {
$y = $x * 7
} elseif ($x = = 8) {
$y = $x * 8
} elseif ($x = = 9) {
$y = $x * 9
} else {
$y = $x * 10
}
}
Print ("if:" (time ()-$s). "sec\ n")
$s = time ()
For ($I = 0; $I < 10000000000; + + $I) {
$x = $I% 10
Switch ($x) {
Case 1:
$y = $x * 1
Break
Case 2:
$y = $x * 2
Break
Case 3:
$y = $x * 3
Break
Case 4:
$y = $x * 4
Break
Case 5:
$y = $x * 5
Break
Case 6:
$y = $x * 6
Break
Case 7:
$y = $x * 7
Break
Case 8:
$y = $x * 8
Break
Case 9:
$y = $x * 9
Break
Default:
$y = $x * 10
}
}
Print ("switch:" (time ()-$s). "sec\ n")
After 1000000000 cycles and adding operations to each judgment condition, we found that switch is more efficient and faster, and the result on my computer is:
/ / if: 301sec
/ / switch: 255sec
Although switch is more efficient, it also needs to be noted that, first of all, the judgment value can only be a number, a floating point number, or a string. Second, each judgment is an ordinary = = judgment, that is, the following judgment is not necessarily the result of your likeness:
$string = "2string"
Switch ($string) {
Case 1:
Echo "this is 1"
Break
Case 2:
Echo "this is 2"
Break
Case'2 stringency:
Echo "this is a string"
Break
}
/ / this is 2
Yes, it is still the problem of type overturning when comparing = =. When comparing string and int values, it turns to int type, and the result of "2string" strong turning is exactly 2. Therefore, when using switch, you should make sure that the comparison values are consistent with the type of each case, otherwise unexpected errors may occur.
Reference code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E7%9A%84switch%E5%92%8Cifelse%E8%B0%81%E6%9B%B4%E5%BF%AB%EF%BC%9F.php
Reference manual: https://www.php.net/manual/zh/control-structures.switch.php
Thank you for your reading, the above is the "PHP switch and ifelse which fast" content, after the study of this article, I believe you on the PHP switch and ifelse which fast this problem has a more profound understanding, the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 219
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.