I need to extract the virtual host name of a HTTP request. Since this willl be done for every request, I´m searching for the fastest way to do this.
Aşağıdaki kodu ve zamanlarda sadece ben okumuştu yollarından bazılarıdır.
Peki, bunun için bazı hızlı bir yolu var mı?
$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";
Ve sonuç olarak zamanlama:
While :0.0886390209198 Regex :1.22981309891 Substr :0.338994979858 Explode :0.450794935226 Replace :0.33411693573