Testing and analysis of the speed of the core function of PHP collection data extraction
Due to program needs, the execution speed of the core part of character extraction in PHP collection was tested.
Tested the three most common extraction methods:
Method 1:
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = strstr( $sourceStr, 'Shape' );
$content = substr( $content, 0, strrpos( $content, 'Word' ) + strlen( 'Word' ) );
return $content;
}
$sourceStr = 'Reject anyone to make any comments in this forum that conflict with the laws of the People's Republic of China';
$debug = new Debug;
$ debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
Extract through relatively low-level character manipulation functions.
Method 2:
php
require "class.debug.php";
function getContent ( $sourceStr )
{
$pattern = "/shape(.*?)yan/is";
preg_match_all( $pattern, $sourceStr, $result );
return $result[1][0];
}
$sourceStr = 'Reject anyone to publish in this forum any form that is inconsistent with the laws of the People's Republic of China Contradictory remarks';
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo;
?>
Use a simple Regular extraction.
Method 3:
require "class.debug.php";
function getContent ( $sourceStr )
{
$content = explode( 'Shape', $sourceStr );
$content = explode( 'Yan', $content[1] );
return $content[0];
}
$sourceStr = 'Reject any People make any comments in this forum that conflict with the laws of the People's Republic of China in any form';
$debug = new Debug;
$debug->startTimer();
for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}
$timeInfo = $debug->endTimer();
echo $timeInfo ;
?>
Extract by splitting the string twice with explode.
My view before testing is: 1 > 2 > 3