Renato Aquino
Renato Aquino

Reputation: 814

There is some way to do this string extraction faster?

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.

The following code and times are just some of the ways I had studied.

So, there is some faster way to do this?

$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";

And as result timings:

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

Upvotes: 0

Views: 215

Answers (3)

Kris
Kris

Reputation: 41867

<?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));
?>

extracted subdomain from subdomain.domain.tld 100000 times in 0.44695 seconds

But that was while encoding video, so it will likely be faster under better circumstances.

Upvotes: 0

Ville Laurikari
Ville Laurikari

Reputation: 29288

You could try the strtok() function:

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

It's faster than a correct version of your while loop, and much more readable.

Upvotes: 5

Alex
Alex

Reputation: 12443

I would do it the substr() way.

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

And I really don't think that the way how you split this string would affect performance in any real-world program. 100 000 iterations is quite huge... ;-)

Upvotes: 3

Related Questions