RE: If the first four characters are "0000", then do {} - timing tests

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: muquaddim@xxxxxxxxx [mailto:muquaddim@xxxxxxxxx] On 
> Behalf Of shiplu
> Sent: Tuesday, January 26, 2010 4:25 PM
> To: Daniel Brown
> Cc: Daevid Vincent; PHP-General
> Subject: Re:  If the first four characters are "0000", 
> then do {} - timing tests
> 
> Ran this code (http://pastie.org/795983)

+1 for using pastie.org. :)

> The result is,
> 
> array of 10,000
> Array
> (
>     [[]] => 5.66168689728
>     [strpos] => 5.70796895027
>     [substr] => 5.92751288414
>     [preg_match] => 6.21515512466
> )

Almost 6 seconds to sort 10k array elements! Are you running this on a
Commodore64 ?!

Not that it probably matters, but I thought that it should be $o[$i]{0}
through $o[$i]{3} 
Note the use of braces not square brackets for string character elements,
no?

Another thing I just noticed, is that we (that is Dan and I) should NOT
have used count()
This is bad form and wasted cycles.

for($i=0;$i<count($o);$i++)

We should have just done a $elements = count($o) at the VERY top and used
$elements instead.

Does anyone know how PHP works internally? If I have 1M elements in the
array, does count() literally just zip through and $i++ them, or is there
some shortcut tally kept in the heap/stack/struct/memory somewhere for each
array?

Updated version:

<?php
for ($elements = 0; $elements < 1000000; $elements++ )
	$o[] =
sprintf('%04d-%02d-%02d',rand(0000,9999),rand(00,99),rand(00,99));
#print_r($o);
echo "array of ".number_format($elements)."\n";
###################################################################
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
       if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
               //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
       }
}
$rank['preg_match'] = (microtime(true) - $time);
###################################################################
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
        if(substr($o[$i],0,4) == "0000") {
                //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
        }
}
$rank['substr'] = (microtime(true) - $time);
###################################################################
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
        if(strpos($o[$i], '0000') === 0) {
                //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
        }
}
$rank['strpos'] = (microtime(true) - $time);
###################################################################
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
		if($o[$i][0]==='0'
	        && $o[$i][1]==='0'
	        && $o[$i][2]==='0'
	        && $o[$i][3]==='0'
	       ) {
	               //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
	       }
}
$rank['[]'] = (microtime(true) - $time);
###################################################################
asort($rank);
print_r($rank);
?>

array of 10,000
Array
(
    [[]] => 0.00380492210388
    [strpos] => 0.0054030418396
    [substr] => 0.00824618339539
    [preg_match] => 0.0103938579559
)

array of 1,000,000  (ran three times)
(
    [[]] => 0.4429500103
    [strpos] => 0.549595832825
    [substr] => 0.847616195679
    [preg_match] => 0.94566321373
)
(
    [[]] => 0.420958995819
    [strpos] => 0.55828499794
    [substr] => 0.86266708374
    [preg_match] => 0.933307886124
)
(
    [[]] => 0.420862197876
    [strpos] => 0.572381019592
    [substr] => 0.855034828186
    [preg_match] => 0.932211875916
)

Notice that without the 'count()' it is significantly faster than before...

array of 1,000,000
Array
(
    [strpos] => 0.805890083313
    [substr] => 1.19799995422
    [preg_match] => 1.25615906715
)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux