PHPWind与Discuz截取字符函数substrs与cutstr性能比较
以掌握使用Benchmark_Iterate类工具的方法。
目录
一,什么是PEAR与Benchmark
二,性能比较代码准备与调试
三,性能比较测试结果
四,性能比较测试总结
五,附性能比较源代码下载
一,什么是PEAR与Benchmark
请参考PHP性能优化系列
第二期 PHP性能优化工具篇Benchmark类调试执行时间
第一期 PHP性能优化准备篇图解PEAR安装
二,性能比较代码准备与调试
测试环境说明
操作系统:Windows xp Service Pack 3
PHP版本:PHP Version 5.2.11
Apache版本:Apache 2.0 Handler
第一步,下载phpwind与discuz!程序源代码
1,下载 phpwind v7.5sp3 论坛版 安装包:简体 GBK
2,下载 Discuz! 7.2_FULL(含UCenter) 简体GBK
第二步,分别获取两个程序的截取字符函数substrs()和cutstr(),其中PHPWind截取字符函数substrs()由两个函数组成,分别是substrs与utf8_trim函数。
复制代码 代码如下:
function substrs($content,$length,$add='Y'){
if (strlen($content)>$length) {
if ($GLOBALS['db_charset']!='utf-8') {
$retstr = '';
for ($i=0;$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) return $string;
}
$string = str_replace(array('&', '"', ''), array('&', '"', ''), $string);
$strcut = '';
if(strtolower($charset) == 'utf-8') {
$n = $tn = $noc = 0;
while($n $t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 $tn = 1; $n++; $noc++;
} elseif(194 $tn = 2; $n += 2; $noc += 2;
} elseif(224 $tn = 3; $n += 3; $noc += 2;
} elseif(240 $tn = 4; $n += 4; $noc += 2;
} elseif(248 $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 $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
$strcut = str_replace(array('&', '"', ''), array('&', '"', ''), $strcut);
return $strcut.$dot;
}
第三步,编写使用PEAR Benchmark_Iterate类调试的代码
复制代码 代码如下:
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$charset = $GLOBALS['db_charset'] = 'gbk';//utf-8
$content = "今年春晚,我特地关注了赵本山的新小品《捐助》,这篇小品对弱势群体的不尊重并无改变,小品讲的是赵本山与其弟子扮演两个捐助者...";
/*phpwind*/
$bench->run(50,"substrs",$content,30);
/*discuz*/
//$bench->run(50,"cutstr",$content,30);
$result = $bench->get();
?>
分别切换substrs和cutstr两个函数并且调用50次获取两个函数执行的平均时间。
三,性能比较测试结果
1,PHPWind程序substrs函数当截取字符编码为GBK的情况
图解:PHPWind程序substrs函数截取GBK编码的字符平均时间在0.0014s-0.0015s之间,纵坐标表示执行的时间,横坐标表示执行的次数,图表中mean表示平均执行时间,注s表示秒
2,Discuz!程序cutstr函数当截取字符编码为GBK的情况
图解:Discuz!程序cutstr函数截取GBK编码的字符平均时间在0.0016s-0.0018s之间
3,PHPWind程序substrs函数当截取字符编码为UTF-8的情况
图解:PHPWind程序substrs函数截取UTF-8编码的字符平均时间在0.001s-0.0012s之间
4,Discuz!程序cutstr函数当截取字符编码为UTF-8的情况
图解:Discuz!程序cutstr函数截取UTF-8编码的字符平均时间在0.0044s-0.0052s之间
四,性能比较测试总结
如上使用PEAR的Benchmark_Iterate类比较了两个截取字符函数的执行性能,在实际的代码开发过程中,为了保证代码的效率与性能,我们可以采取同样的方法来分析函数的执行性能或问题,这个非常有用!
本文通过比较PHPWind与Discuz两个社区论坛产品的截取字符函数substrs与cutstr的执行性能情况为例,谈谈PEAR Benchmark_Iterate类在实际开发中的应用,请继续关注下期的PHP性能优化系列。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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: 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.

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.

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.

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.

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

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.

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.
