Php Performance Optimization-php language-level performance optimization 1

Source: Internet
Author: User

Php Performance Optimization-php language-level performance optimization 1


First of all, we need to know the reasons for affecting the performance of php? That is

1. Under what circumstances will php performance problems occur?

1php syntax is improperly used (including some services that can be processed using functions provided by php itself)

2. I used php to do something that I was not good.

3. the servers connected with the php language are not powerful (of course, if localhost is used, your local configuration is poor, please change the version. Haha)

4php's own short board (PHP itself cannot do it)

5. What we do not know)

2 Introduction to php performance problems-solutions to php performance problems

From difficulty to depth:

1. Php language-level performance optimization

2. Performance Optimization for Php peripheral problems (for example, mysql nginx | apache)

3 Php senior Analysis and Optimization (ps mainly refers to the underlying c code)


The following is an example of php language-level performance optimization. We will test the content in the title and then write two files: bad. php and goods. php.

We want to test the operation of merging two arrays (test tool apache AB test)

Bad. php

Ideas:

Add array 1 to the target array one by one;

Then, traverse array 2 and compare whether the elements of array 2 appear in array 1. If not, insert them to the target array; otherwise, ignore them.

  


Goods. php

Ideas:

Generate two arrays randomly, in disordered order

Then use array_merge to merge

   


3. Use AB to test the speed of two php scripts: (ps: an astonishing time has begun)

Bad. php


Goods. php <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141205/20141205084010231.png" alt = "">


For these two tests, we can see that the difference is not half past one. When I run bad. php, I obviously feel the computer fan, you know!

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

Goods. php responds to 4050 requests per second. Each request is processed for 24 ms.

Ps: If you want your friends to use the bad statement, get rid of it and be careful not to know it!



4. Reasons

I was surprised by this result, but the fact is just in front of me. No way, why is this effect? Let's continue to look at it:

*. Php (php code) ----- parse (the zend engine performs a row-by-row scan to change to a syntax that zend can recognize) ----> exprs

----- Parser (resolved to opcode) -----> opcodes ------ exec (executed final output) -------> output


We can know that zend needs to be scanned for a php file and parsed to opcode for output execution. The problem lies here. When we use php's own functions, it actually belongs to the zend engine, therefore, zend is easy to parse, and the speed is fast (goods. php), but when the execution is bad. php is slow, because reading the code written by others is certainly not reading the code written by yourself. You are right, so it is better to use originality instead of making your own wheels!


To put it another way, this is why some Php extensions (such as apc) cache opcode. Because of this, scanning and parsing are missing, it must be faster!


5. Summary

Optimization: less code writing and more php capabilities

Performance problems:

The code written by myself is redundant, hard to read, and of low performance.

Why is performance low?

Php code needs to be compiled and parsed into the underlying language. This process will be processed every time, with high overhead.

Good method:

Use php built-in variables, constants, and functions (spl can provide you with easy-to-use functions)


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.