PHP Performance Optimization PHP language-level performance optimization

Source: Internet
Author: User
Tags php language prepare rand shuffle


The first thing we need to know about this problem is what's going to affect PHP's performance. Is

1 What happens when PHP performance issues occur.

Improper use of 1php syntax (including some services that can be handled using PHP's own functions)

2 did what it wasn't good at using the PHP language.

3 in the PHP language linked to the server does not give force (of course, if it is localhost is your local configuration is poor ha, suggest change this, haha)

4php its own short board (PHP itself can not do)

5 problems that we don't know (embarrassed)

2 PHP Performance issues Brief introduction of PHP performance problem solving direction

from shallow to deep, the difficulty degree is:

1 PHP language-level performance optimization

2 Performance optimization for PHP peripheral problems (e.g. MySQL Nginx|apache)

3 PHP Language Senior Analysis, optimization (PS mainly refers to the bottom of the C code)


Below for PHP language-level performance optimization to do a case discussion, the title of the content to do a test, the 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.

After that, iterate through array 2 to see if the elements of array 2 appear in array 1, or if they are not inserted into the target array, ignore

<span style= "FONT-SIZE:14PX;" ><?php
//Prepare two random array of contents
$arr 1 = $arr 2 = $arr _merged = Array ();
The next two arrays are randomly assigned for
($i =0; $i <rand (1000,2000); $i + +) {
	$arr 1[] = 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 generated two arrays, in an upset order

After that, use the array_merge merge

<span style= "FONT-SIZE:14PX;" ><?php
//Prepare two random array of contents
$arr 1 = $arr 2 = range (1000);
$arr _merged = Array ();
Next randomly assigns two array
shuffle ($arr 1);
Shuffle ($arr 2);

$arr _merged = Array_merge ($arr 1, $arr 2);

Var_dump ($arr _merged);
</span>


3 speed of using AB to test two PHP scripts: (PS: Amazing moment begins)

bad.php


goods.php



For these two tests, we clearly see that the difference is not a little bit ah, in the implementation of bad.php I obviously feel the computer fan Weng, you know.

Bad.php responds to 174 requests per second, processing 572ms per request

Goods.php responds to 4,050 requests per second, processing 24ms per request

PS: If you want to small partners in the use of bad writing, quickly get rid of it, be careful not to your leader know oh.



4 Investigate the reason

I am also surprised at the result, but the fact is in front of, there is no way ah, then exactly why there is such an effect, let's continue to explore the truth:

*.php (PHP code)-----Scanner (Zend Engine-by-line scan changes to Zend-recognizable syntax)----> Exprs

-----parser (resolves to opcode)-----> opcodes------EXEC (perform final output)-------> Output


Can know a php file execution Zend need to scan first, and then parse for opcode in the implementation of output, the problem is here, when we use the function of PHP itself in fact, he belongs to the Zend engine, so the Zend parse up more smoothly, speed that call a quick (goods.php) , but when the implementation of the bad.php will be slower, because read other people write code must not read their own written code fast everyone said right, so or use the original, do not build their own wheels better ah.


To say a little more, that's why so many PHP extensions (such as APCs) are now caching opcode because it's less scanning and parsing, which is definitely fast.


5 Summary

Optimization point: Write less code, use PHP itself to provide the ability

Performance Issues:

Write your own code more redundant, poor readability, and low performance

why performance is low.

The PHP code needs to be compiled and parsed into the underlying language, and the process is processed once for each request, with a large overhead.

Good way:

Use PHP built-in variables, constants, functions (SPL can bring you like a useful function)


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.