Home> php教程> php手册> body text

对PHP采集数据提取核心函数的速度的测试与分析

WBOY
Release: 2016-06-13 11:21:00
Original
755 people have browsed it

对PHP采集数据提取核心函数的速度的测试与分析
由于程序需要,于是对PHP采集中的字符提取的核心部分进行了执行速度的测试。
测试了三种最常见的提取办法:
方法一:
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = strstr( $sourceStr, '形' );
$content = substr( $content, 0, strrpos( $content, '言' ) + strlen( '言' ) );
return $content;
}
$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i {
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
通过比较低级的字符操作函数进行提取.
方法二:
require "class.debug.php";
function getContent ( $sourceStr )
{
$pattern = "/形(.*?)言/is";
preg_match_all( $pattern, $sourceStr, $result );
return $result[1][0];
}
$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i {
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
使用一个简单的正则来提取.
方法三:
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = explode( '形', $sourceStr );
$content = explode( '言', $content[1] );
return $content[0];
}
$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i {
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
通过两次explode分裂字符串来提取.
测试前我的观点是: 1 > 2 > 3

source:php.cn
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
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!