Home Backend Development PHP Tutorial PHPWind and Discuz intercept character function substrs and cutstr performance comparison_PHP tutorial

PHPWind and Discuz intercept character function substrs and cutstr performance comparison_PHP tutorial

Jul 21, 2016 pm 03:22 PM
discuz and by use function character performance intercept master Compare

To master the method of using Benchmark_Iterate class tools.

Table of Contents
1. What is PEAR and Benchmark
2. Performance comparison code preparation and debugging
3. Performance comparison test results
4. Performance comparison test summary
5 , with performance comparison source code download

1. What is PEAR and Benchmark

Please refer to the PHP performance optimization series
The second issue of PHP performance optimization tools chapter Benchmark class debugging execution time
Illustrated PEAR installation in the first issue of PHP performance optimization preparation

Second, 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

First step, download phpwind and discuz! program source code
1, download phpwind v7.5sp3 forum version installation package: Simplified GBK
2, download Discuz! 7.2_FULL (including UCenter) Simplified GBK

The second step is to obtain the character interception functions substrs() and cutstr() of the two programs, among which PHPWind intercepts the character function substrs () consists of two functions, namely substrs and utf8_trim functions.

Copy code 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]) > 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]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
$strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut);
return $strcut.$dot;
}

The third step is to write debugging code using the PEAR Benchmark_Iterate class
Copy code The code is as follows:

require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$ charset = $GLOBALS['db_charset'] = 'gbk';//utf-8
$content = "This year's Spring Festival Gala, I paid special attention to Zhao Benshan's new sketch "Donation". This sketch disrespected the disadvantaged groups. There is no change, the skit is about Zhao Benshan and his disciples playing two donors...";

/*phpwind*/
$bench->run(50,"substrs",$content ,30);
/*discuz*/
//$bench->run(50,"cutstr",$content,30);

$result = $bench-> get();
?>

Switch the substrs and cutstr functions respectively and call them 50 times to obtain the average execution time of the two functions.
3. Performance comparison test results

1. When the substrs function of the PHPWind program intercepts the character encoding as GBK
PHPWind程序substrs函数当截取字符编码为GBK的情况
Illustration: The average time for the PHPWind program substrs function to intercept GBK-encoded characters is between 0.0014s-0.0015s. The ordinate represents the execution time, and the abscissa represents the number of executions. In the chart mean represents the average execution time, note s represents seconds

2, Discuz! program cutstr function when intercepting character encoding is GBK
Discuz!程序cutstr函数当截取字符编码为GBK的情况

Illustration: The average time taken by the Discuz! program cutstr function to intercept GBK-encoded characters is between 0.0016s-0.0018s

3. The PHPWind program substrs function intercepts character encoding. In the case of UTF-8
PHPWind程序substrs函数当截取字符编码为UTF-8的情况

Illustration: The average time for the PHPWind program substrs function to intercept UTF-8 encoded characters is between 0.001s-0.0012s

4, Discuz! program cutstr function when intercepting Character encoding is UTF-8
Discuz!程序cutstr函数当截取字符编码为UTF-8的情况
Illustration: The average time taken by Discuz! program cutstr function to intercept UTF-8 encoded characters is between 0.0044s-0.0052s

Fourth, performance comparison test summary

As above, the execution performance of the two interception character functions was compared using the Benchmark_Iterate class of PEAR. In actual During the code development process, in order to ensure the efficiency and performance of the code, we can use the same method to analyze the execution performance or problems of the function. This is very useful!

This article takes as an example a comparison of the execution performance of the interception character functions substrs and cutstr of two community forum products, PHPWind and Discuz, to talk about the application of the PEAR Benchmark_Iterate class in actual development. Please continue to pay attention to the PHP performance in the next issue. Optimized series.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324703.htmlTechArticle to master the method of using Benchmark_Iterate class tools. Contents 1. What are PEAR and Benchmark 2. Performance comparison code preparation and debugging 3. Performance comparison test results 4. Performance comparison test...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

What is Bitget Launchpool? How to use Bitget Launchpool? What is Bitget Launchpool? How to use Bitget Launchpool? Jun 07, 2024 pm 12:06 PM

BitgetLaunchpool is a dynamic platform designed for all cryptocurrency enthusiasts. BitgetLaunchpool stands out with its unique offering. Here, you can stake your tokens to unlock more rewards, including airdrops, high returns, and a generous prize pool exclusive to early participants. What is BitgetLaunchpool? BitgetLaunchpool is a cryptocurrency platform where tokens can be staked and earned with user-friendly terms and conditions. By investing BGB or other tokens in Launchpool, users have the opportunity to receive free airdrops, earnings and participate in generous bonus pools. The income from pledged assets is calculated within T+1 hours, and the rewards are based on

What is the performance impact of converting PHP arrays to objects? What is the performance impact of converting PHP arrays to objects? Apr 30, 2024 am 08:39 AM

In PHP, the conversion of arrays to objects will have an impact on performance, mainly affected by factors such as array size, complexity, object class, etc. To optimize performance, consider using custom iterators, avoiding unnecessary conversions, batch converting arrays, and other techniques.

Things to note when Golang functions receive map parameters Things to note when Golang functions receive map parameters Jun 04, 2024 am 10:31 AM

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.

See all articles