Simple Data Cache Technology - 3

zhaozj2021-02-12  162

Simple Data Cache Technology - 3PHP Applications Performance Optimization Use PHP programming is very easy to learn this programming language and its rich libraries. Even if the functions you need to use are not very understanding, we can also guess how to complete a specific task. Although PHP is very easy to learn, we still need to spend some time to learn some Programming skills of PHP, especially with performance and memory occupancy. In PHP, there are many tips to reduce our memory, and improve the performance of the application. In this article, we will make a brief introduction to the analysis of the PHP application, how to change the script code and the various parameter values ​​before and after the optimization. By setting the timing in the program, we can obtain a set of data on the execution speed of the program, which can be used to discover the bottleneck in the program, and how to optimize, improve the performance of the application. Perhaps the reader has heard of the PEAR library. We will use the PEAR library to create an example in which you need to use, which is the easiest way to analyze the existing code, which makes us analyze the code without using commercial products. The name of the library we want to use is Pear :: Benchmark, which is very useful for analysis and performance testing of code. This library provides a class name Benchmark_Timer () that can record a function call and the time between the next function call. When testing the performance of the code, we can get a detailed script execution result, which is very simple, as shown below: include_once ("Benchmark / Timer.php"); $ BENCH = New Benchmark_timer; $ BENCH-> Start ); $ bench-> setmarker ('start of the script'); // is now in sleep state a few minutes sleep (5); $ bench-> stop (); // Get the analysis information from the timer Print_R ($ Bench -> getProfiling ());?> The output of the above code is as follows: array ([0] => array ([name] => start [Time] => 1013214253.05751200 [DIFF] => - [Total] = > 0) [1] => Array ([Name] => Start of the script [Time] => 1013214253.05761100 [DIFF] => 9.8943710327148E-05 [Total] => 9.8943710327148e-05) [2] => array ([Name] => Stop [Time] => 1013214258.04920700 [DIFF] => 4.9915959835052 [Total] => 4.9916949272156) The above number seems to be a set of messy numbers, but if the program is large, these numbers are very It is useful.

Perhaps the readers can also guess that the first representation of the array is actually calling the Benchmark_Timer () method, such as $ Bench-> Start (), $ BENCH-> SetMarker () and $ Bench-> stop (), The numbers related to these expressions are quite simple. Now let's study these numbers carefully: [0] => Array ([name] => start [Time] => 1013214253.05751200 [DIFF] => - [Total] = > 0) TIME table refers to when is the unix TimeSTAMP that is called by the start () method of Benchmark_Timer (), the DIFF representation indicates the time interval between the call and the last call, because there is no last time, therefore A dash, TOTAL table refers to the total time of the self-test starting before the code before this particular call. Let's take a look at the output of the next array: [1] => Array ([name] => Start of the script [Time] => 1013214253.05761100 [DIFF] => 9.8943710327148E-05 [Total] => 9.8943710327148e 05) From the above number, we can see that after calling $ BENCH-> Start (), the program runs 9.8943710327148E-05 seconds (that is, 0.0000989 seconds), start calling $ bench-> setmarker (....) . A true performance test experience although the above example is good, it is really a good example in which you have optimized your site code design. Below I will use my own personal experience as a personal experience of the website technician to explain how to solve performance. I don't understand the code used by the website, because it is based on special needs. After years of development, one of the modules including the website conversion code, another module records the use of the website, and other modules have each Each role. My main developers and websites realize that the code's code needs to be optimized, but it is not clear that there is a problem. In order to complete the task as soon as possible, I started to study the main script code of the website, and add some $ BENCH-> SetMarker () commands in all script code and it, and then analyze the output of $ BENCH-> getProfiling (), and The result is very surprised, the original problem is in a function call to a conversion code that obtains a particular language name (e.g. ENGLISH), this function is used for hundreds of times on each page. Each time you call this function, script code queries a MySQL database and gets a real language name from a database table. So our information created a buffer system. After a short 2 days, we have improved the performance of the system, and the page browsing in the first week increased by 40%. Of course, this is just an example of an analysis code to improve Internet applications or Internet site performance. Performance Test Function Call When you analyze a script or web page (and it contains files), although Benchmark_timer () is especially useful, it is not scientific, because we must obtain the analysis of the script, and it is not for some Class or function call. The class in the Pear :: Benchmark library is known as the class of Benchmark_iterator, which can solve this problem well, which can display its analysis information for a specific function or class method.

转载请注明原文地址:https://www.9cbs.com/read-6701.html

New Post(0)