Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of performance optimization in PHP development specification

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of performance optimization in the PHP development specification". In the daily operation, I believe that many people have doubts about the method of performance optimization in the PHP development specification. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is the method of performance optimization in the PHP development specification?" Next, please follow the editor to study!

I. Specification

Performance is the key factor of whether the website runs well or not. the performance and efficiency of the website affect the operating cost and long-term development of the company. Writing high-quality and efficient code is a necessary quality for every developer, and it is also our good professional accomplishment.

2. Factors affecting performance

A, business demand

Demand rationality

Integration of requirements and systems

Is the business benefit brought by demand directly proportional to the cost of demand development?

Risks caused by demand

B, Web server

Concurrent processing capacity

High load capacity

The ability of load balancing

Processing ability of dynamic content and static content

Web server deployment

C, DataBase server

Concurrent access

Deployment of database server

Whether the shema architecture of the database and the table design are reasonable

Data retrieval

D, operating system

E, client request

F, program / language

Third, the index of analytical performance

Run time of the program 2

The memory consumed by the running of the program

Parallel processing per unit time

Processing of disk IO

IV. The goal of optimizing performance

Fast, concurrent, low resource consumption (memory, disk IO, CPU load)

V. the principle of optimizing performance

Optimization of server configuration

Rationalization of server deployment

Business demand is reasonable and proportional to the commercial value of output

Architecture available, maintainable, extensible

The correctness, simplicity and logical rationality of the program.

Constantly analyzing performance bottlenecks

Constantly refactoring existing code

Priority of optimization: program- > database- > web sersver- > os- > client

VI. Optimization

A. Program optimization

Variable

1, variable size, pay attention to variable size is the most effective way to save memory, for data from user forms, databases and file caches need to control the size of variables. Because the data to be processed by cpu comes from memory.

2, the period of validity of variables, using the unset () function to log out unwanted variables is a good habit. Logging out some unneeded variables immediately can improve memory usage.

3, copy variables, try not to copy variables, otherwise it will bring twice the memory consumption, even if you copy variables, you should immediately log out the original variables.

4, variable type, initialize a variable, please pay attention to its variable type, a variable had better have only one type state in the process of execution. For array variables, initialize the declaration as follows: $a = array ()

5. Temporary variables are temporary storage for dealing with business logic, which need to consume memory. If the use of temporary variables is over, please log out immediately, especially in the execution process of some procedure code, for some functions, if the business is very complex, you also need to log out temporary variables immediately.

6. Static variables. For some variables that need to be generated by complex business, if they are generated and used many times during the execution of the program, you can consider using static variables to reduce the number of cpu execution of the program.

7. The performance of variables: local variables > global variables > class attributes > undefined variables.

Cycle

1. Try to reduce the number of cycles.

2. Try to reduce the level of escape of the cycle, no more than three layers.

3. Avoid having too much business logic in the loop.

4. Do not loop the include file

5. Do not cycle through database operations.

6. Give priority to using foreach, which is more efficient than for/while

7. Do not put count/strlen/sizeof in the conditional statement of the for loop For ($ipreg_replace ()-> epreg ())

8. Give full play to the maximum effect of trim and replace substr. $filepath=trim ($filename,'/'). /'

9. Isset/empty although the functions of the two functions are different, it is recommended to use empty () in the same case.

10. The functions of the two functions of isfile/file_exist are different. File_exist can judge whether a file exists or a directory exists. In the same case, is_file is recommended.

File

1. Reduce the number of file inclusions and disk IO

2. Use a full path, or a relative path that is easy to convert. Avoid searching on include_path

3. The number of lines of code in the file should not exceed 2000.

4. Require_once/include_once is less efficient than require/include, so you need extra to check whether this file has been called by the system. Because they are very slow to call under an opcode cache

5. Requie/require_once is used for program execution files and include/include_once is used for cache files. Include is more efficient than require.

6. To optimize the automatic file loading mechanism in spl, please refer to yii.

7. When the class library file is loaded, whether to consider whether the class has been instantiated or not, the singleton pattern of design pattern can be considered.

8. Concurrency of reading and writing of documents

object-oriented

1. Control the number of instances created

2. Give priority to the use of constants and similar constants

3. Give priority to static variables and static attributes

4. The structure of the class is reasonable.

5. Face-image interface programming

6. Encapsulation change point

7. Rely on abstraction, not on details

8. Give priority to static members

9. the interface of the class is clear and stable, the responsibility of the class is single, and the communication between the class and the class is reasonable.

10. the advantage of using constants is parsing at compile time, and the hash table is smaller without extra overhead, so internal lookups are faster. Class constants only exist in a specific "namespace", so the hash name is shorter and the code is cleaner, making debugging more convenient.

String

1. Use single quotation marks instead of double quotation marks to quote strings; avoid retrieving variables in strings

Operation

1. Replace i=i+1 with iTunes 1. It accords with the habit of cAccord plus, and it is still efficient.

2. The efficiency of + $I is higher than that of + $I

Array

1. Try to avoid cyclic nesting assignment in multi-dimensional arrays.

2. Use $array ['name'] to access the array, and prohibit $array [name] / $array ["name"]

Judge

1. For logic judgment, please give priority to switch. For cases where there are relatively many business logic, please choose if/else to improve the readability of the code.

2. Try to control the number of if/else judgments. If there are too many, please consider function optimization or code optimization.

3. Try to use identity for comparative judgment. The efficiency of identity is higher than or equal to, and it can also avoid some types of cast errors.

4. If/else and _ &. Choose the form of & & when judging a single statement. The efficiency of & & is higher than that of if/else, as follows:

If ($a = = 1) {

$b = 2

} can be selected as ($a = = 1) & & $b = 2

Caching

1. Use php accelerator to buffer opcode

2. Memcache/nosql for example

3. Use in-memory database,

4. Use file caching

5. Buffer function 5

Other

1. Reduce the use of @ symbols, which seriously affects performance

2. Close remote resource connections such as database, ftp, socket, etc., and clean up these resources at the right time.

B, database optimization

1. Reasonable business needs

2. Optimization of database schema architecture

3. Vertical and horizontal database subtables

4. Index optimization, query optimization

5. Third-party open source retrieval tool (sphinx)

6. The use of master-slave database server.

C, Web server optimization (not sorted out yet, there is a corresponding Web server optimization manual)

D, operating system optimization (not sorted out yet, there is a corresponding OS optimization manual)

E, front-end optimization

1. Reasonable html structure

2. While reasonable html and css, consider the reasonable design of Css and reduce http requests.

3. Consider whether the split is reasonable and reduce the number of http requests while reasonably html and javascript

4. Optimize javascript code to make users have a good experience.

5. Optimize high concurrency requests according to http protocol

7. Performance testing tools

Web Server

Ab

Http_load

PHP

Apd

Xdebug

Mysql

Explain

Profiler

At this point, the study on "what is the method of performance optimization in the PHP development specification" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report