What are the differences in the speed of implementing the same function in php functions? Why is there a difference in speed?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.