Phpwind and Discuz intercept character functions Substrs and CUTSTR performance comparison _php techniques

Source: Internet
Author: User
Tags benchmark ord pear php performance tuning
To master the method of using the Benchmark_iterate class tool.

Directory
One, what is pear and benchmark
Second, performance comparison code preparation and debugging
Third, performance comparison test results
Four, performance comparison test summary
Five, with performance comparison source code download

One, what is pear and benchmark

Please refer to the PHP Performance Tuning series
Phase II PHP Performance Tuning tool benchmark class debugging execution time
The first phase of PHP performance optimization preparation article Diagram Pear installation

Second, performance comparison code preparation and debugging

Test environment Description
Operating system: Windows XP Service Pack 3
PHP Versions: PHP version 5.2.11
Apache version: Apache 2.0 Handler

The 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 Intercept character function substrs () and Cutstr () of two programs, in which the Phpwind intercept character function substrs () is composed of two functions, substrs and Utf8_trim functions respectively.
Copy Code code 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 | | (<= $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 (<= $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;
}

Step three, write code that uses the Pear Benchmark_iterate class to debug
Copy Code code as follows:

<?php
Require_once "benchmark/iterate.php";
$bench = new Benchmark_iterate;
$charset = $GLOBALS [' db_charset '] = ' GBK ';//utf-8
$content = "This year's gala, I specially paid attention to Zhao Benshan's new sketch" donation ", this essay on the disadvantaged groups of disrespect and no change, the sketch is about Zhao Benshan and his disciples play two donors ...";

/*phpwind*/
$bench->run ("Substrs", $content, 30);
/*discuz*/
$bench->run ("Cutstr", $content, 30);

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

Switch Substrs and CUTSTR two functions separately and call 50 times to get the average time of two function executions.
Third, performance comparison test results

1,phpwind program Substrs function when intercepting character encoding as GBK

graphic: Phpwind program Substrs function intercepts GBK encoded characters mean time between 0.0014s-0.0015s, ordinate represents the execution time, the horizontal axis represents the number of executions, and the mean in the chart represents the average execution time, and the note s represents the second

2,discuz! program Cutstr function when intercepting character encoding as GBK

illustration : discuz! program CUTSTR function intercepts GBK encoded characters mean time between 0.0016s-0.0018s

3,phpwind program Substrs function when intercepting character encoding as UTF-8

illustration : Phpwind program Substrs function intercepts UTF-8 encoded characters mean time between 0.001s-0.0012s

4,discuz! program Cutstr function when intercepting character encoding as UTF-8

illustration : discuz! program CUTSTR function intercepts UTF-8 encoded characters mean time between 0.0044s-0.0052s

Four, performance comparison test summary

For example, using Pear's benchmark_iterate class to compare the execution performance of two intercept character functions, in the actual code development process, in order to ensure the efficiency and performance of the code, we can take the same approach to analyze the performance of the function or problem, this is very useful!

This article compares Phpwind and discuz two community forum product's intercept character function substrs and Cutstr's execution performance situation as an example, discusses the Pear Benchmark_iterate class in the actual development application, please continue to focus on the next period PHP performance Optimization series.

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.