PHP string length judgment more efficient method _php tutorial

Source: Internet
Author: User
Experienced programmers found that PHP judged the length of the string, using Isset () at a faster speed than strlen () and performing more efficiently.
namely:
Copy CodeThe code is as follows:
$str = ' aaaaaa ';
if (strlen ($STR) > 6)
Vs
if (!isset ($str {6})

With the example of a simple test, the situation is basically true, isset () efficiency is almost 3 times times the strlen ().
Example:
Copy CodeThe code is as follows:
Using the Strlen method
$arr = "123456";
$sTime = Microtime (1);
if (strlen ($arr) > 6) {
Echo 1;
}
echo microtime (1)-$sTime;

Output:0.00035595893859863
Copy CodeThe code is as follows:
Use Isset ($arr {}) mode
$arr = "123456";
$sTime = Microtime (1);
if (!isset ($arr {6})) {
echo "1\r\n";
}
echo microtime (1)-$sTime;

Output:0.00019097328186035

Why Isset () speed is faster than strlen ()
The strlen () function function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C for storing PHP variables). However, because strlen () is a function, it is somewhat slower, because function calls go through a number of steps, such as lowercase letters (lowercase, PHP does not differentiate between function names), and hash lookups, which follow the called functions.
In some cases, using the isset () technique can speed up the execution of your code. Because with Isset () as a language structure, it means that its execution does not require function lookups and lowercase letters. In other words, you're actually not spending much overhead in the top-level code that examines the length of the string.

So call Isset () faster than strlen ().

http://www.bkjia.com/PHPjc/736830.html www.bkjia.com true http://www.bkjia.com/PHPjc/736830.html techarticle experienced programmers found that PHP judged the length of the string, using Isset () at a faster speed than strlen () and performing more efficiently. That is: Copy the Code code as follows: $str = ' aaaaaa '; if ( .....

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