What are the differences in the speed of implementing the same function in php functions? Why is there a difference in speed? What are the differences in the speed of implementing the same function in php functions? Why is there a difference in speed?
Reply content:
What are the differences in the speed of implementing the same function in php functions? Why is there a difference in speed?
There are many "Steps" to answer this question... In PHP, two functions do the same thing, but almost all of them are aliases (such as sizeof vs. count, strstr vs. strchr ). In this case, their performance is exactly the same. If you already have a function with the same function, why should PHP develop a function with the same speed?
However, if the definition of the "function" is relaxed, we can find that there is indeed such a type of thing, the PHP system function is slower than the other syntax. A typical example is to check whether the length of a string exceeds the specified value. For example, it cannot exceed 1000.
One method is if (strlen ($ str) <= 1000 ). PHP checks whether the len member of this string object is greater than 1000. So we won't count this string every time. The time complexity is O (1 ).
Another atypical approach is if (! Isset ($ str [1000]). The time complexity is also O (1), but you will find that it is several times faster than strlen.
Why is the speed difference between the two functions so much? Because PHP will process isset and convert the above expression to just a few VM commands. This is a bit like Java's intrinsics. Although call functions can complete isset tasks, they can inline function-related commands into code for faster speed.
Strlen has no such treatment. To call strlen, you have to go through the PHP routine call system function program. Hundreds of codes are required. Therefore, the speed is much slower than isset.
However, note that strlen and isset are not fully implemented in the same way, but they are used to check the string length. Both of them are competent. So .. Although this example is far-fetched, I think it best fits your problem standards.