PHP Performance optimizations for PHP language-level performance optimization 1

Source: Internet
Author: User
PHP performance optimization for PHP language level performance optimization


First of all, what are the reasons for the performance of PHP that is affected by this problem? That is

1 What happens when PHP performance issues occur?

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

2 Use PHP language to do what it is not good at

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

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

5 questions we don't know about (embarrassing)

2 php performance questions about PHP performance issues in the direction of resolution

From the difficulty degree from shallow to deep respectively:

1 Php language-level performance optimizations

2 Performance optimizations for PHP peripheral issues (e.g. mysql nginx|apache)

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


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

  
    


goods.php

Ideas:

Randomly generates two arrays in a scrambled order

After that, use Array_merge to merge

  
    


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)


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