Performance Comparison between the substrs and cutstr functions of PHPWind and Discuz truncation character functions

Source: Internet
Author: User
In the fourth phase of the PHP performance optimization series, how to use the PEARBenchmark tool to obtain the function execution performance is described in PHPWind and Discuz! The truncation functions substrs and cutstr in the two community forums are used as examples to analyze the execution performance of the two functions to master 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.

The 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

The code is as follows:


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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.