Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有什么方法可以更快地提取字符串?_Php_Algorithm_String_Pattern Matching - Fatal编程技术网

Php 有什么方法可以更快地提取字符串?

Php 有什么方法可以更快地提取字符串?,php,algorithm,string,pattern-matching,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 <

我需要提取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";
$hostname=“alphabeta.gama.com”;
$iteractions=100000;
//中途测试
$time\U start=微时间(真);
对于($i=0;$i<$i分数;$i++){
$vhost=”“;
而($i<20)&($hostname{$i}!='.')$vhost.=$hostname{$i++};
}
$time\U end=微时间(真);
$timewhile=$time\u end-$time\u start;
//正则表达式检验
$time\U start=微时间(真);
对于($i=0;$i您可以尝试strtok()函数:

$vhost = strtok($hostname, ".")
它比while循环的正确版本更快,可读性也更高。

我会使用substr()方法

我真的不认为分割字符串的方式会影响任何实际程序的性能。100000次迭代相当大…;-)


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


但这是在编码视频时发生的,因此在更好的情况下可能会更快。

您确定这是代码中较慢的部分吗?性能优化应该首先对最慢的代码进行,否则会浪费时间。同意,但现在是时候做点小事情了;)@伊万-没错。如果通过数据库连接来跟踪此操作,则是在浪费时间。请记住,对于每个请求,您不会执行10万次。即使使用最慢的方法,每100k请求也会增加1.2秒。您的while检查不正确。您继续使用
$i
变量,但需要一个新变量。这将起作用:
for($k=0;$k。这是所有选项中最慢的选项。在迭代开始之前获取主机名的长度将使执行时间减少一半,但仍然是最慢的选项。Strtok计时:0。234637975693@Ville好主意@雷纳托,这是对最慢的一个大约2微秒的改进。干得好。顺便问一下,既然速度对你来说非常重要,你是在使用某种PHP加速器吗?嘿,维尔,我接受你的答案。它并不比while选项快,但它确实更具可读性。谢谢Alex、you、Ivan、Byron和任何其他认为在现实世界的应用程序中“这种优化”是浪费时间的人都是对的。我只是在寻找这个问题的答案:“有没有更快的方法可以做到这一点?”谢谢你的回答。好的。出于兴趣,substr($host,0,strstr($host,“.”)的速度有多快?strpos而不是strstr应该更快。呵呵,我错过了你的代码中已经包含了这个事实,但我注意到Alex的strstr使用有点尴尬,我想尝试一下。
$vhost = strtok($hostname, ".")
$vhost = substr($host, 0, strstr($host, "."));
<?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));
?>