> -----Original Message----- > From: parasane@xxxxxxxxx [mailto:parasane@xxxxxxxxx] On > Behalf Of Daniel Brown > Sent: Tuesday, January 26, 2010 8:21 AM > To: Daevid Vincent > Cc: John Taylor-Johnston; PHP-General > Subject: Re: If the first four characters are "0000", then do {} > > On Mon, Jan 25, 2010 at 22:51, Daevid Vincent > <daevid@xxxxxxxxxx> wrote: > >> -----Original Message----- > >> From: parasane@xxxxxxxxx [mailto:parasane@xxxxxxxxx] On > >> Behalf Of Daniel Brown > >> Sent: Monday, January 25, 2010 6:43 PM > >> To: John Taylor-Johnston > >> Cc: PHP-General > >> Subject: Re: If the first four characters are > "0000", then do {} > >> > >> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston > >> <John.Taylor-Johnston@xxxxxxxxxxxxxxxxxxxxx> wrote: > >> > I am reading the manual: > http://ca.php.net/manual/en/ref.strings.php > >> > > >> > $mydata->restored = "0000-00-00"; > >> > >> <?php > >> > >> $o[] = '0942-23-23'; > >> $o[] = '0000-00-00'; > >> $o[] = '1238-00-00'; > >> $o[] = '0001-23-45'; > >> $o[] = '0000-11-22'; > >> > >> for($i=0;$i<count($o);$i++) { > >> if(preg_match('/^[0]{4,}\-/U',$o[$i])) { > >> echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; > >> } > >> } > >> ?> > > > > Holy macaroni. Talk about overkill! > > > > if (substr($mydata->restored,0,4) == "0000") { } > > Overkill? > > <?php > > $o[] = '0942-23-23'; > $o[] = '0000-00-00'; > $o[] = '1238-00-00'; > $o[] = '0001-23-45'; > $o[] = '0000-11-22'; > > $now = microtime(); > > for($i=0;$i<count($o);$i++) { > if(preg_match('/^[0]{4,}\-/U',$o[$i])) { > //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; > } > } > > echo (microtime(1) - $now)."\n"; > > > $later = microtime(); > > for($i=0;$i<count($o);$i++) { > if(substr($o[$i],0,4) == "0000") { > //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; > } > } > > echo (microtime(1) - $later)."\n"; > > ?> > > Sample Output: > > 1264522257.0001 > 1264522257 > > The preg_match() method, which is more expandable and adaptable > than relying on static position, took less than one-ten-thousandths of > a second longer to calculate than substr(). Well allow me to retort... :) Your test was for 5 measly array elements. Just for S&G I tried it with the strpos one too and modified your test slightly to handle bigger arrays... <?php for ($i = 0; $i < 1000000; $i++ ) $o[] = sprintf('%04d-%02d-%02d',rand(0000,9999),rand(00,99),rand(00,99)); #print_r($o); echo "array of ".number_format($i)."\n"; ################################################################### $now = microtime(true); for($i=0;$i<count($o);$i++) { if(preg_match('/^[0]{4,}\-/U',$o[$i])) { //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; } } $rank['preg_match'] = (microtime(true) - $now); ################################################################### $later = microtime(true); for($i=0;$i<count($o);$i++) { if(substr($o[$i],0,4) == "0000") { //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; } } $rank['substr'] = (microtime(true) - $later); ################################################################### $after = microtime(true); for($i=0;$i<count($o);$i++) { if(strpos($o[$i], '0000') === 0) { //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL; } } $rank['strpos'] = (microtime(true) - $after); ################################################################### asort($rank); print_r($rank); ?> array of 10,000 Array ( [strpos] => 0.00766682624817 [substr] => 0.0116670131683 [preg_match] => 0.0124950408936 ) array of 100,000 Array ( [strpos] => 0.0817799568176 [substr] => 0.120522975922 [preg_match] => 0.125612974167 ) array of 1,000,000 Array ( [strpos] => 0.805890083313 [substr] => 1.19799995422 [preg_match] => 1.25615906715 ) I ran out of memory with more than 1M array elements. But yes, I will concede that the speed difference is minimal, even at 1M elements. Although the docs are right that strpos is about 2x as fast... > Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P By the way, I loved your book "Da Vinci Code" ;-p ÐÆ5ÏÐ http://daevid.com Ezekiel 25:17. "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of the darkness. For he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know I am the Lord when I lay my vengeance upon you!" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php