PHP Performance Optimization Learning Notes

Source: Internet
Author: User

Writing code

1. Use built-in functions as much as possible
2. Time complexity compared to built-in functions, with low complexity for example loops 200,000 times-Test isset and array_key_exists time consuming
Contrast isset.php, array_key_exists.php
3. Use as few magical functions as possible
Compare __get.php no__get.php
4. No, no, @ Error suppression symbol
View at.php, view opcode with vlb extensions
 1  cli:php-dvld.active=1-dvld.execute=0 at. php View opcode  2  //   4  file_get_contents  (' 1.txt '  5  @file_get_contents  (' 1.txt '); // opcode a bit more logic    
5. Proper use of memory and regular expressions
Use Unset () to release unused memory in a timely manner and use regular expressions sparingly
6. Reduce compute-intensive operations
Big data calculation, log analysis, etc.
7. Be sure to use quoted strings for key values
View arr.php
1<?PHP2 //define (' key ', ' Imooc ');3 $arr= [4' Key ' = ' Hello ',5' Imooc ' = ' http://www.imooc.com/'6 ];7 8 Echo $arr[Key];9 /*Ten key is not quoted One Key first will look for the constant key, if any, take the value of the constant key, and then retrieve the corresponding value of the array.  A if not found, resolves the array internal key - */ 
 8. Performance optimizations for PHP peripheral issues  
Linx server optimization, file storage, memory cache, network
Reduce network requests:
A. You can use curl_multi_* () to send a set of request #此方案会有问题, although you can send multiple requests at one time, but return Back depends on the longest time-consuming request
b. Use the swoole extension

9. PHP interface, compressed PHP interface output such as gzip
Lee: for the client side to receive data faster;



10. Use the Xhprof performance test
view xhprof.php

1<?PHP2 //refer to the example/simple.php in the XHPROF project3 4 #Open5 xhprof_enable ();6 7 #The script finishes executing8 register_shutdown_function(function(){9 Ten     $data=xhprof_disable (); One  A     require' Xhprof_lib/utils/xhprof_lib.php '; -     require' Xhprof_lib/utils/xhprof_runs.php '; -  the     //generate an analysis file -     $obj=NewXhprofruns_default (); -     $run _id=$obj->save_run ($data, ' Test '); -     Var_dump($run _id); +  -});
11. Turn on Opcache
PHP Execution Process
1 *.php script is saved by progressive scan analysis of the underlying Zend engine to Zend's own recognizable syntax
2 Zend Self-recognizable grammar is then parsed into opcodes (the code that will eventually be executed)
3 final Execution opcodes output
Attach PHP execution flowchart

The paper mentions the file download: Http://files.cnblogs.com/files/loveyouyou616/yh.zip

PHP Performance Optimization Learning Notes

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.