PHP's performance has been improving. However, if used inappropriately, or an inattentive, or may step into the PHP internal implementation of the pit. I ran into a performance problem a few days ago.
The thing is this, a colleague feedback one of our interfaces takes 5 seconds to return each time, we review the code together, the "surprise" discovery actually called a read cache operation in the loop (about 900 times), and the cached key didn't change, so we moved the code out of the loop. , again, the interface return time drops to 2 seconds, alas! Although it has been raised 1 time times, it is obviously not a result we can accept!
The amount of code that has a performance problem is not large, and after we have eliminated the IO problem, we write a test code, and the problem soon recurs.
Copy Code code as follows:
<?php
$y = "1800";
$x = Array ();
For ($j =0 $j <2000; $j + +) {
$x []= "{$j}";
}
For ($i =0 $i <3000; $i + +) {
if (In_array ($y, $x)) {
Continue
}
}
?>
shell$ time/usr/local/php/bin/php test.php
Real 0m1.132s
User 0m1.118s
SYS 0m0.015s
Yes, we're using string numbers, and that's what you get out of the cache! So this is deliberately turned into a string (if the number is direct, this problem will not occur, you can verify yourself). We can see that the time spent 1 seconds, only 3,000 cycles, after the SYS also doomed us to use strace will not get what effective information.
shell$ Strace-ttt-o xxx/usr/local/php/bin/php test.php
shell$ less XXX
We only see the delay between these two system calls is very large, but do not know what to do? The helpless, fortunately, under the debugging of Linux, in addition to Strace and Ltrace (of course, dtrace,ptrace, not in the scope of this article discussed, omitted).
Reference: Strace is used to track the system call or signal generation of a process, and ltrace is used to track the process call library functions (via IBM developerworks).
To eliminate the interference factors, we assign the $x directly to the array ("0″," 1″, "2″,......) To avoid excessive malloc calls that affect the results. Perform
shell$ ltrace-c/usr/local/php/bin/php test.php
As shown in Figure 2
We see the library function __strtol_internal call is very frequent, reached 94%, too exaggerated, and then I looked at the library function __strtol_internal is why, the original is Strtol alias, simple to say is the string conversion growth and shaping, You can guess that the PHP engine has detected that this is a string-type number, so expect them to be converted to a growth integer to compare, this conversion process consumes too much time, we again execute:
Copy Code code as follows:
shell$ ltrace-e "__strtol_internal"/usr/local/php/bin/php test.php
can easily catch a lot of such a call to the following figure, to this, the problem found, In_array this loose comparison, will be two characters of the number string first converted to long integer type before the comparison, but do not know the performance of the above.
Know the crux of the problem, we solve a lot of the solution, the simplest is for In_array plus the third parameter is true, that becomes a strict comparison, but also to compare types, so as to avoid the conversion of PHP smart-aleck type, run up really fast, the code is as follows:
Copy Code code as follows:
<?php
$y = "1800";
$x = Array ();
For ($j =0 $j <2000; $j + +) {
$x []= "{$j}";
}
For ($i =0 $i <3000; $i + +) {
if (In_array ($y, $x, True)) {
Continue
}
}
?>
Copy Code code as follows:
shell$ time/usr/local/php/bin/php test.php
Real 0m0.267s
User 0m0.247s
SYS 0m0.020s
Many times faster!!! You can see that the SYS time consuming is almost not much changed. Once again, we are going to assign the $x directly to the ltrace, excluding the interference from the malloc call, because we actually pull it out of the cache in the actual application, so there is no such loop in the sample code to request memory.
Executed again
Copy Code code as follows:
shell$ ltrace-c/usr/local/php/bin/php test.php
The following figure:
__ctype_tolower_loc takes up the most time! Checked the library function. __ctype_tolower_loc: A simple understanding is to convert a string to lowercase, does this mean that in_array comparison strings are case-insensitive? In fact, this function call already and we this in_array feeling is not very connected, about the realization of in_array, or to see the source code of PHP, probably understand more thoroughly, well, can't say, welcome to communicate with me, write the wrong place please a lot of treatise.
——————— 2013.08.29 Split Line ——————————
The night again turned over the following PHP 5.4.10 source code, the interest of In_array really big ah, haha, located in./ext/standard/array.c's 1248th line, you can see that he called the Php_search_array function, the following Array_ Serach is also tuned this, but the last parameter is different! After some tracking, in the case of In_array, he eventually called the function zendi_smart_strcmp (sure is a "smart" function) for comparison, located in./zend/zend_ OPERATORS.C, we use Ltrace to catch a large number of conversion into an integral type of operation is that is_numeric_string_ex behavior.
function is_numeric_string_ex is in./zend/zend_ Defined in Operators.h, after a heap of judgments and transformations, the strtol is called in line 232, which is the system function we mentioned in the article, which converts the string into an integral type, with the figure having the truth