PHP functions to achieve the same function but the speed of the difference is what, why the speed is different?

Source: Internet
Author: User
PHP functions to achieve the same function but the speed of the difference is what, why the speed is different?

Reply content:

PHP functions to achieve the same function but the speed of the difference is what, why the speed is different?

The problem of "stepping" more, but I still dare to answer it ... There are examples of two functions doing the same thing in PHP, but almost all because they are aliases (such as sizeof vs. Count,strstr vs. STRCHR). In this case, their performance is exactly the same. You want to, if you already have a function of the same function, why should PHP develop a function that is identical (and at a different speed)?

But if the definition of "function" is relaxed, it can be found that there is such a thing, PHP system function is slower than the other syntax. A typical example is checking whether a string is longer than the specified value. Say no more than 1000.

One way is if (strlen ($STR) <=1000). PHP internally checks if the Len member of this string object is greater than 1000. So not every time the string is counted, the time complexity is O (1).
Another atypical practice is if (!isset ($str [1000])). The time complexity is also O (1), but you will find it several times faster than the strlen test.

Why are the two functions so much worse? Because PHP handles isset, the above expression is converted to just a few virtual machine instructions. This is a bit like Java's intrinsics, although the call function can complete isset tasks, but the function of the relevant instructions inline into the code, the speed can be faster.
And strlen has no such treatment. Call strlen to go to the PHP routine call system function program, frequently hundreds of code. So the speed is much slower than isset.

But note that strlen and isset do not all have the same functionality, but are used to check the string length here, both are competent. So.. Although this example has some far-fetched, but I think it is most in line with your problem standard.

  • 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.