有一些方法可以更快地完成这个字符串提取吗?

时间:2009-11-05 21:16:45

标签: php algorithm string pattern-matching

我需要提取HTTP请求的虚拟主机名。 由于这将针对每个请求完成,我正在寻找最快的方法。

以下代码和时间只是我学习的一些方法。

那么,有一些更快的方法可以做到这一点吗?

$hostname = "alphabeta.gama.com";

$iteractions = 100000;

//While Test

$time_start = microtime(true);
for($i=0;$i < $iteractions; $i++){
    $vhost = "";
    while(($i < 20) && ($hostname{$i} != '.')) $vhost .= $hostname{$i++};
}

$time_end = microtime(true);
$timewhile = $time_end - $time_start;

//Regexp Test
$time_start = microtime(true);
for($i=0; $i<$iteractions; $i++){
    $vhost = "";
    preg_match("/([A-Za-z])*/", $hostname ,$vals);
    $vhost = $vals[0];
}
$time_end = microtime(true);
$timeregex = $time_end - $time_start;

//Substring Test
$time_start = microtime(true);
for($i=0;$i<$iteractions;$i++){
    $vhost = "";
    $vhost = substr($hostname,0,strpos($hostname,'.'));
}
$time_end = microtime(true);
$timesubstr = $time_end - $time_start;

//Explode Test
$time_start = microtime(true);
for($i=0;$i<$iteractions;$i++){
    $vhost = "";
    list($vhost) = explode(".",$hostname);
}
$time_end = microtime(true);
$timeexplode = $time_end - $time_start;

//Strreplace Test. Must have the final part of the string fixed.
$time_start = microtime(true);
for($i=0;$i<$iteractions;$i++){
    $vhost = "";
    $vhost = str_replace(".gama.com","",$hostname);
}
$time_end = microtime(true);
$timereplace = $time_end - $time_start;

echo "While   :".$timewhile."\n";
echo "Regex   :".$timeregex."\n";
echo "Substr  :".$timesubstr."\n";
echo "Explode :".$timeexplode."\n";
echo "Replace :".$timereplace."\n";

结果时间:

While   :0.0886390209198
Regex   :1.22981309891
Substr  :0.338994979858
Explode :0.450794935226
Replace :0.33411693573

3 个答案:

答案 0 :(得分:5)

您可以尝试使用strtok()函数:

$vhost = strtok($hostname, ".")

它比你的while循环的正确版本更快,更具可读性。

答案 1 :(得分:3)

我会用substr()方式做。

$vhost = substr($host, 0, strstr($host, "."));

我真的不认为你分割这个字符串的方式会影响任何真实世界程序的性能。 100 000次迭代非常庞大......; - )

答案 2 :(得分:0)

<?php
$iterations = 100000;
$fullhost = 'subdomain.domain.tld';

$start = microtime(true);

for($i = 0; $i < $iterations; $i++) 
{
    $vhost = substr($fullhost, 0, strpos($fullhost, '.'));
}

$total = microtime(true) - $start;
printf( 'extracted %s from %s %d times in %s seconds', $vhost, $fullhost, $iterations, number_format($total,5));
?>

在0.44695秒内从subdomain.domain.tld提取子域100000次

但那是在编码视频时,所以在更好的情况下它可能会更快。