I don't know how many people will read it, but I think it is valuable to share some good things. the biggest advantage of using PHP programming is that it is very easy to learn this programming language and its rich library. Even if we don't know much about the functions we need, we can guess how to complete a specific task. Although PHP is very easy to learn, we still need to spend a little bit to release it. I don't know how many people will read it, but we must share some good things to find it valuable.
The biggest advantage of using PHP programming is that it is very easy to learn this programming language and its rich library. Even if we don't know much about the functions we need, we can guess how to complete a specific task.
Although PHP is very easy to learn, it still takes us a little time to learn some PHP programming skills, especially those related to performance and memory usage. In PHP, there are many tips that can help us reduce memory usage and improve application performance. In this article, we will briefly introduce the analysis of PHP applications, how to change script code, and various parameter values before and after optimization.
By setting the timing program in the program and executing the code repeatedly, we can obtain a set of data about the program execution speed, which can be used to find bottlenecks in the program, and how to optimize and improve application performance.
Maybe you have heard of the PEAR Library. We will use the PEAR library to create an example for analysis. this is also the simplest method for analyzing existing code, it allows us to analyze code without using commercial products.
The name of the library we want to use is PEAR: Benchmark, which is very useful for code analysis and performance testing. This library provides a class named Benchmark_Timer (), which records the time between a function call and the next function call. When testing the code performance, we can get a detailed script execution result, which is very simple, as shown below:
Include_once ("Benchmark/Timer. php ");
? $ Timer = new Benchmark_Timer ;? $ Begin-> start ();
? $ Marker-> setMarker ('Start of the script'); sleep for several minutes
Sleep (5 );? $ Timer-> stop (); // Obtain analysis information from the timer
Print_r (? $ Response-> getProfiling ());
?>
The numbers above seem to be a mess of numbers, but they are very useful if the program is larger.
Maybe the readers can guess that the first table in the array is actually calling the Benchmark_Timer () class method, for example
? $ Tags-> start (),? $ Marker-> setMarker () and? $ Tables-> stop (). the numbers related to these table categories are quite simple. now let's take a closer look at these numbers:
The time table refers to the UNIX timestamp when the start () method of Benchmark_Timer () is called. the diff table indicates the time interval between this call and the last call, because there is no previous one, a broken number is displayed. The total table refers to the total time of code running from the test to the specific call. Next let's take a look at the output of the next array:
From the preceding figure, we can see that? $ Success-> start (), the program runs for 9.8943710327148E-05 seconds (that is
0.0000989 seconds) and then start calling? $ Marker-> setMarker (....).
A real performance test experience
Although the above example is good, it is really not a good example of deciding how to optimize your site code design. Next I will explain how to solve the performance problems with my personal experience as a website technician.
I don't quite understand the code used by the website, because it is developed based on special requirements over the years. one module of the website contains the website conversion code, and the other module records the usage of the website, other modules have their respective functions. I and the main website developers both realized that the website code needs to be optimized, but they do not know where the problem is.
To complete the task as soon as possible, I started to study the main script code of the website and added some? $ Marker-> setMarker () command, and then analyze? $ Response-> getProfiling () output, and was surprised by the result. the original problem was a function call that obtained the conversion code in a specific language name (for example, en stands for english, this function is used hundreds of times on every page. Each time you call this function, the script code queries a MySQL database and obtains the real language name from a database table.
So we created a buffer system for this type of information. After just two days of work, we have greatly improved the system performance. in the first week, the page views increased by 40%. Of course, this is just an example of how analysis code can improve the performance of Internet applications or internet websites.
Performance Test function call
Although Benchmark_Timer () is particularly useful in analyzing a script or webpage (and its contained files), it is not scientific, because we must load the script multiple times to obtain the analyzed data, and it is not called for a class or function.
PEAR: another class in the Benchmark library called Benchmark_Iterator can solve this problem well. it can display the analysis information of a specific function or class method. Its purpose is to get consistent results from the test, because we know that if a script is run once, its running time is 10 seconds, it does not mean that every running time is always 10 seconds.
In any case, let's see some examples:
// Database connection code
Include_once ("DB. php ");
? $ Dsn = array (
'Phptype '=> 'mysql ',
'Hostspec '=> 'localhost ',
'Database' => 'Database _ name ',
'Username' => 'User _ name ',
'Password' => 'password'
);
? $ Dbh = DB: connect (? $ Dsn); function getCreatedDate (? $ Id)
{
Global? $ Dbh;>? $ Stmt = "SELECT created_date FROM users WHERE id =? $ Id ";
// Here PEAR: DB is used
? $ Created_date =? $ Dbh-> getOne (? $ Stmt );
If (PEAR: isError (? $ Created_date) |
(Empty (? $ Created_date ))){
Return false;
} Else {
Return? $ Created_date;
}
} Include_once 'benchmark/Iterate. php ';
? $ Iterator = new Benchmark_Iterate; // run the getDate function 10 times.
? $ Response-> run (10, 'getcreateddate', 1); // Print the analysis information
Print_r (? $ Response-> get ());
?>
The preceding numbers are easy to understand. mean entries indicate the average time of the getCreatedDate () function to run for 10 times. The actual test should be performed at least 1000 times, but the results obtained in this example are sufficient to explain the problem.
Conclusion
It is hoped that the majority of readers will learn how to quickly analyze PHP code through this article. Here, I also want to remind readers that analyzing code is not a simple task, because we must master a lot of features related to this language. The code used to add timing in the code helps to find slow functions. using repeated methods, we can find the correct method to optimize the code.
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.