PHP Performance Optimization summary

Web site performance is a key factor in the running is good, the performance and efficiency of the site's impact on the company's operating costs and long-term development, the preparation of a high quality and efficient code is that each developer must have the quality, but also our good professional qualities.

How to optimize PHP performance?

First, the variables (important)      

1), variable size, the variable size is noted that the most effective means to save memory, the data from the form user, database and file cache needs to control the size of the variable. Because the data to be processed is derived from the cpu memory

2), the variable is valid, use the unset () function does not require the cancellation of the variables is a good habit to immediately write-off some unwanted variables can improve memory usage.

3) to copy a variable, try not to copy the variables, otherwise it will bring 1 times the memory consumption, even if the variable copy should be immediately written off existing variable.

4), the variable type, variable initialization note its variable type, preferably only one type of a state variable during execution. For an array variable, initialization statement, as follows: $ a = array ();

5), temporary variables are temporary storage processing business logic, these are the need to consume memory. If the end of the temporary variables please log out immediately, particularly in the implementation process some of the procedural code, for some functions, if the business is very complex, we also need immediate cancellation of temporary variables

6), static variables, some variables generated by the complex business needs, if generated and used many times in the course of execution of the program, consider using static variables, reduce the number of program execution cpu

7), the performance variables: Local variables> Global Variables> Class Properties> undefined variables.

 

Second, the circulation (important)

1), to minimize the number of cycles.

2) to minimize the level of absconding cycle, do not exceed three.

3), to avoid excessive service logic in the circulation.

4) Do not include documents circulation

5) Do not circulate perform database operations.

6), the foreach preferably used, which is higher than for / while efficiency

Conditional statement 7), not to count / strlen / sizeof placed for cycle For ($ i = 0, $ count = count ($ array); $ i <$ count; $ i ++) {} do not use for ($ i = 0; $ i <count ($ array); $ i ++) {};

8)、 for($i=$total;$i>0;$i–){}性能好于for($i=0;$i<$total;$–){}

9), the loop body to maintain the business logic clear

 

Third, the function (important)

1), the function clear responsibilities, a function only do one thing, do not hybridity too much business logic

2), the function code is not more than 20 lines of the body, on the contrary, consider splitting.

3), preferably used built-in functions php

4), constants and function while competent one thing, using the constant priority.

         E.g:

            · phpversion() < PHP_VERSION

            · get_class() < __CLASS__

            · is_null() < NULL ===

5), echo performance is better than the print, a plurality of input variables when using echo $ str, $ str1, no. Connector

6), $ _SERVER [REQUEST_TIME] Alternatively Time ();

7), the replacement string strtr () -> str_replace () -> preg_replace () -> epreg ();

8) plays trim for maximum effectiveness, replace substr. $ Filepath = trim ($ filename, '/') '/'.;

9), Isset / empty although two functional capabilities vary, but in the same circumstances recommended empty ()

10), the function isfile / file_exist two different functions, determining whether either file_exist file exists, it may be determined whether a directory exists, it is recommended to use in the same circumstances is_file

 

Fourth, the file (important)

1) to reduce the file contains the number, to reduce disk IO

2), use the full path, or can be easily converted to a relative path. Find avoid include_path

3), the number of lines of code file should not exceed 2000 lines

4), Require_once / include_once less efficient than require / include, the need for additional system has been called to view this file. Because they call at a very slow opcode cache

5), program execution files requie / require_once, cache files include / include_once. Include efficiency better than require

6) to optimize file spl automatic loading mechanism, may participate by yii

7), the library file is loaded, whether to consider whether the class is already instantiated, contemplated uses singleton design pattern

8), file read and write concurrency

 

Fifth, object-oriented (important)

1), control the number of instances created

2), preferably used constants, class constant

3), the priority Example static variables, static properties

4), a rational structure of the class

5), as the programming interface surface

6), the package change point

7), dependent on the abstract, it is not dependent on the details

8), priority use of static members

9), an interface, clear and stable, single responsibility class, the class with reasonable traffic class

10), the benefits of using the compiler constants parsing, no overhead hash table smaller, faster and look inside exists only in a particular class constants "namespace", so the name is shorter hash code is cleaner and make it easier to debug

 

Six String

1), alternatively in single quotes cited double quotes; search character string variables to avoid

 

Seven operations

1), replaced by i + = 1 i = i + 1. Habits in line with c / c ++, the efficiency is higher

2), it is higher than the efficiency ++ $ i ++ $ i, - $ i Likewise [/ hide]

 

Eight arrays

1), multi-dimensional arrays try not nested loop assignment;

2), using $ array [ 'name'] array access, prohibiting $ array [name] / $ array [ "name"]

 

IX judgment (important)

1), the logic determines the priority mode please use a switch, for the case of relatively large business logic select if / else, code readability

2), try to control if / else number determination, if too many consider optimization function or code optimization

3), to make use of the identity determined for comparison, the identity is more efficient than equal, but also to avoid errors of several casts

4), if / else _ && and a single select statement determines the form && && efficient than if / else, as follows:

if ($a == 1) {

$b = 2;

}

Alternatively to ($ a == 1) && $ b = 2;

 

X. Cache

1), using php accelerators, buffering opcode

2)、 例用memcache/nosql

3), using in-memory database,

4), use the file cache

5), buffer function

 

XI, other

1), less the @ symbol, seriously affect the performance

2) timely close the connection of remote resources such as databases, ftp, socket, etc., timely clean up these resources

 

Twelve, database optimization (important)

1) reasonable business need love

2), database schema architecture optimization

3), the vertical and horizontal sub-library sub-table

4), index optimization, query optimization

5), a third party open source search tools (Sphinx)

6), from the master server using a database.

 

XIII, front end optimization

1) reasonable structure html

2), while reasonable html and css, taking Css design reasonable, reduce http requests

3) reasonably html simultaneously with the java script, consider splitting is reasonable to reduce the http request

4) to optimize java script code so that users have a good experience

5), according to the http protocol, optimizing high concurrent requests

 

Fourth, performance testing tool (important)

Just search for "how to check website performance xxx" can be on Baidu.

1)、Web Server

2), from

3)、http_load

4)、PHP

5)、apd

6)、xdebug

7)、Mysql

8)、explain

9)、profiler

 

Reprinted from: https: //baagee.vip/index/article/id/71.html

 

Guess you like

Origin www.cnblogs.com/guliang/p/11890509.html