First of all, what are the reasons for the performance of PHP that is affected by this problem? That is
The Following is an example discussion of the performance optimization of PHP language level, The title inside said content carries on a test, next to write two files bad.php, goods.php
We're going to test the operation of merging two arrays (test tool Apache AB test)
bad.php
Ideas:
First, the array 1 is added to the target array one by one;
After that, the array 2 is traversed, the elements in the array 2 are compared to the array 1, or if none are inserted into the target array, otherwise ignored
<span style= "FONT-SIZE:14PX;" ><?php//prepare two content random array $arr1 = $arr 2 = $arr _merged = Array ();//Next randomly assign two arrays for ($i =0; $i <rand (1000,2000); $i + +) {$a Rr1[] = rand ();} for ($i =0; $i <rand (1000,2000); $i + +) {$arr 2[] = rand ();} Start Loop Compare foreach ($arr 1 as $v) {$arr _merged[] = $v;} foreach ($arr 2 as $v) {if (!in_array ($v, $arr _merged)) {$arr _merged[] = $v;}} Var_dump ($arr _merged);</span>
goods.php
Ideas:
Randomly generates two arrays in a scrambled order
After that, use Array_merge to merge
<span style= "FONT-SIZE:14PX;" ><?php//prepare two random array of contents $ARR1 = $arr 2 = Range (n, a), $arr _merged = Array (), or//next randomly assigns two arrays shuffle ($arr 1); shuffle ($arr 2); $arr _merged = Array_merge ($arr 1, $arr 2); Var_dump ($arr _merged);</span>
3 using AB to test the speed of two PHP scripts: (PS: Amazing moment started)
bad.php
goods.php
For these two tests, we obviously see this difference is not a little bit ah, in the implementation of bad.php when I obviously feel the computer fan Weng, you know!
bad.php response to 174 requests per second, processing 572ms per request
goods.php response to 4,050 requests per second, processing 24ms per request
PS: If you want a small partner in the use of bad, get rid of it, be careful not to your leader know Oh!
4 The reason
I am also very surprised at the result, but the fact is in the eyes, there is no way ah, then why in the end there is such an effect, we continue to explore:
*.php (PHP code)-----Scanner (Zend Engine progressive scan changes to Zend recognized syntax)----> Exprs
-----parser (resolves to opcode)-----> opcodes------EXEC (perform final output)-------> Output
Can know that a php file execution Zend need to scan first, and then resolve to opcode in the execution of output, the problem is here, when we use PHP's own function when actually he belongs to the Zend engine, so Zend parse it is more handy, Speed that call a fast (goods.php), but when the execution of bad.php is slower, because reading someone else's code must not read their own code to write fast everyone said right, so still use the original, do not build wheels better ah!
To say a little more, this is why a lot of PHP extensions (such as APC) to cache opcode because this will be less scanning and parsing of the link, it must be fast!
5 Summary
Optimization point: Write less code, and use PHP's own ability to provide
Performance issues:
Write your own code more redundant, poor readability, and low performance
Why is performance low?
PHP code needs to be compiled and parsed into the underlying language, and this process is processed once per request, with a high overhead.
Good method:
Use PHP built-in variables, constants, functions (SPL can give you the ability to function as if it is useful)