To learn how to use the Benchmark_Iterate tool.
Directory
1. What is PEAR and Benchmark?
2. Performance Comparison code preparation and debugging
Iii. Performance Comparison Test Results
Iv. Summary of performance comparison tests
V. Download performance comparison source code
1. What is PEAR and Benchmark?
See PHP performance optimization Series
Phase ii php performance optimization tool Benchmark class debugging and execution time
Phase I PHP performance optimization preparation illustration PEAR Installation
2. Performance Comparison code preparation and debugging
Test Environment Description
Operating System: Windows xp Service Pack 3
PHP Version: PHP Version 5.2.11
Apache version: Apache 2.0 Handler
Step 1: Download phpwind and discuz! Program source code
1. Download The phpwind v7.5sp3 Forum installation package: Simplified GBK
2. Download Discuz! 7.2_FULL (including UCenter) Simplified GBK
Step 2: Obtain the truncation character functions substrs () and cutstr () of the two programs respectively. The PHPWind truncation character function substrs () consists of two functions: substrs and utf8_trim.
Copy codeThe Code is as follows:
Function substrs ($ content, $ length, $ add = 'y '){
If (strlen ($ content)> $ length ){
If ($ GLOBALS ['db _ charset']! = 'Utf-8 '){
$ Retstr = '';
For ($ I = 0; $ I <$ length-2; $ I ++ ){
$ Retstr. = ord ($ content [$ I]) & gt; 127? $ Content [$ I]. $ content [++ $ I]: $ content [$ I];
}
Return $ retstr. ($ add = 'y '? '..':'');
}
Return utf8_trim (substr ($ content, 0, $ length). ($ add = 'y '? '..':'');
}
Return $ content;
}
Function utf8_trim ($ str ){
$ Hex = '';
$ Len = strlen ($ str)-1;
For ($ I = $ len; $ I >=0; $ I-= 1 ){
$ Ch = ord ($ str [$ I]);
$ Hex. = "$ ch ";
If ($ ch & 128) = 0 | ($ ch & 192) = 192 ){
Return substr ($ str, 0, $ I );
}
}
Return $ str. $ hex;
}
Function cutstr ($ string, $ length, $ dot = '...'){
Global $ charset;
If (strlen ($ string) <= $ length ){
Return $ string;
}
$ String = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ string );
$ Strcut = '';
If (strtolower ($ charset) = 'utf-8 '){
$ N = $ tn = $ noc = 0;
While ($ n <strlen ($ string )){
$ T = ord ($ string [$ n]);
If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
$ Tn = 1; $ n ++; $ noc ++;
} Elseif (194 <=$ t & $ t <= 223 ){
$ Tn = 2; $ n + = 2; $ noc + = 2;
} Elseif (224 <=$ t & $ t <= 239 ){
$ Tn = 3; $ n + = 3; $ noc + = 2;
} Elseif (240 <=$ t & $ t <= 247 ){
$ Tn = 4; $ n + = 4; $ noc + = 2;
} Elseif (248 <=$ t & $ t <= 251 ){
$ Tn = 5; $ n + = 5; $ noc + = 2;
} Elseif ($ t = 252 | $ t = 253 ){
$ Tn = 6; $ n + = 6; $ noc + = 2;
} Else {
$ N ++;
}
If ($ noc >=$ length ){
Break;
}
}
If ($ noc> $ length ){
$ N-= $ tn;
}
$ Strcut = substr ($ string, 0, $ n );
} Else {
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Strcut. = ord ($ string [$ I]) & gt; 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
}
}
$ Strcut = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ strcut );
Return $ strcut. $ dot;
}
Step 3: Compile the code for debugging using the PEAR Benchmark_Iterate class
Copy codeThe Code is as follows:
<? Php
Require_once "Benchmark/Iterate. php ";
$ Tenant = new Benchmark_Iterate;
$ Charset = $ GLOBALS ['db _ charset'] = 'gbk'; // UTF-8
$ Content = "this year's gala, I paid special attention to Zhao Benshan's new article" donation ". This article does not change the disrespect for the disadvantaged groups, zhao Benshan and his disciples played two donors... ";
/* Phpwind */
$ Response-> run (50, "substrs", $ content, 30 );
/* Discuz */
// $ Response-> run (50, "cutstr", $ content, 30 );
$ Result = $ response-> get ();
?>
Switch the substrs and cutstr functions and call them 50 times to obtain the average execution time of the two functions.
Iii. Performance Comparison Test Results
1. When the substrs function of the PHPWind program is encoded as GBK
Illustration: The substrs function of the PHPWind program intercepts the average time of GBK-encoded characters between 0.0014s and 0.0015s. The ordinate represents the execution time, the abscissa represents the execution times, and the mean represents the average execution time in the chart, note s indicates seconds
2, Discuz! When the program cutstr function intercepts characters and encodes them into GBK
Illustration: Discuz! The program cutstr function intercepts GBK-encoded characters for an average time between 0.0016s and 0.0018s.
3, PHPWind program substrs function when intercepting character encoding for UTF-8
Illustration: PHPWind program substrs function intercept UTF-8 encoding characters average time between 0.001s-0.0012s
4, Discuz! Program cutstr function when intercepting characters encoded as UTF-8
Illustration: Discuz! The program cutstr function intercepts the average length of characters encoded in the UTF-8 between 0.0044s-0.0052s
Iv. Summary of performance comparison tests
The Benchmark_Iterate class using PEAR compares the execution performance of two truncated character functions. In the actual code development process, to ensure code efficiency and performance, we can use the same method to analyze the execution performance or problems of functions. This is very useful!
This article compares the execution performance of the substrs and cutstr truncation character functions of PHPWind and Discuz community forums to discuss the application of the PEAR Benchmark_Iterate class in actual development, continue with the PHP performance optimization series in the next phase.