search
HomeBackend DevelopmentPHP TutorialPHPWind and Discuz intercept character function substrs and cutstr performance comparison_PHP tutorial

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$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;
}

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
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
discuz database error怎么解决discuz database error怎么解决Nov 20, 2023 am 10:10 AM

discuz database error的解决办法有:1、检查数据库配置;2、确保数据库服务器正在运行;3、检查数据库表状态;4、备份数据;5、清理缓存;6、重新安装Discuz;7、检查服务器资源;8、联系Discuz官方支持。解决Discuz数据库错误需要从多个方面入手,逐步排查问题原因,并采取相应的措施进行修复。

discuz是什么意思discuz是什么意思Aug 23, 2023 am 10:27 AM

Discuz是一个功能强大的开源论坛软件,可以帮助用户快速搭建和管理一个社区论坛,提供了一套完整的论坛系统解决方案,Discuz是由名为Comsenz的中国公司开发和维护的,并且在全球范围内广泛使用。Discuz还有一个庞大的用户社区,可以提供技术支持和经验分享。

怎么去掉discuz版权怎么去掉discuz版权Feb 24, 2023 am 09:15 AM

去掉discuz版权的方法:1、找到并打开“header_common.htm”文件,删掉“Powered by Discuz!”内容;2、找到并打开“footer.htm”文件,删掉“Powered by ME”内容即可。

discuz论坛是什么discuz论坛是什么Jul 10, 2023 am 11:03 AM

discuz论坛是一种网络论坛软件,也称BBS,它是一种用于在互联网上建立论坛社区的程序系统。只哟中功能强大的论坛软件,可以帮助用户建立一个专业、完善的论坛社区,并且可以实现多种功能,如搭建用户注册、登录、查看主题、发布帖子、发表评论、设置版主等功能,让用户可以轻松地进行论坛社区的管理和维护。

什么是discuz什么是discuzAug 23, 2023 am 10:24 AM

discuz是一种功能强大、灵活性高、安全稳定的开源论坛软件,是一个基于PHP和MySQL的在线社区平台,提供了一个完整的论坛系统,包括帖子、主题、用户管理、权限控制等功能。Discuz还具备良好的用户体验和界面设计,以及庞大的开发者社区,可以为用户提供帮助和支持。

discuz如何修改头像discuz如何修改头像Aug 08, 2023 pm 03:53 PM

discuz修改头像的方法:1、登录Discuz后台,在网站根目录下找到“admin.php”或者“admin”目录并登录;2、进入用户管理,可以在左侧或者顶部的导航菜单中找到并点击进入;3、搜索用户,使用搜索功能来找到特定的用户;4、修改头像,在编辑页面,可以找到头像的选项并上传新的头像;5、保存修改;6、刷新页面即可。

discuz登录失败怎么解决discuz登录失败怎么解决Aug 03, 2023 pm 02:09 PM

discuz登录失败解决方法:1、仔细检查输入的用户名和密码是否正确,并尝试重置密码;2、确保浏览器允许使用Cookie,并将Discuz网站添加到信任的网站列表中;3、通过论坛首页上的“联系我们”或“举报”链接找到管理员的联系方式并解决;4、查看服务器日志来确定是否存在配置问题。

discuz附件在哪discuz附件在哪Aug 08, 2023 pm 02:08 PM

discuz附件在网站的根目录下,其路径类似于"/home/wwwroot/forum/data/attachment",在这个目录下,每个附件都会有一个唯一的文件名,以确保文件的唯一性和安全性,附件通常是用户在帖子或私信中上传的文件,例如图片、音频或文档,附件的上传和管理是论坛功能的重要组成部分,使用户能够分享和交流各种类型的内容。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.