Re: [PHP] If the first four characters are "0000", then do {} - timing tests
On Tue, Jan 26, 2010 at 19:48, Daevid Vincent wrote: > > 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. This is certainly correct, but it should also be noted that my original code used it once (thus, it was in fine form) and it was only to demonstrate a small field of results. Still, though a different discussion, it's worth reminding folks to audit their own code to see how many places they have unnecessary calls to count() when cycling through the same array in multiple places. Someone should start a "Pointers For Newbies, Reminders For Oldies" thread on the list and let everyone participate with what should be obvious - but are often overlooked - points within coding practice that can cause the programmer to develop bad habits and bad code. Who here would like to volunteer for the task? ;-P -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] If the first four characters are "0000", then do {}
> -Original Message- > From: Daevid Vincent [mailto:dae...@daevid.com] > Sent: 26 January 2010 03:52 > > if (substr($mydata->restored,0,4) == "") { } > > Or in your very specific case you could do the harder way and note > that > strings work like simple arrays too in a way, so $mydata- > >restored{0} > through $mydata->restored{3} should all be '0' ( note the {} and not > [] ). Sorry, this is out of date and wrong. [] is the currently recommended way to do string indexing, and {} is deprecated. See the Note at http://php.net/manual/en/language.types.string.php#language.types.string.substr. Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Leeds Metropolitan University, C507, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] If the first four characters are "0000", then do {} - timing tests
> -Original Message- > From: muquad...@gmail.com [mailto:muquad...@gmail.com] On > Behalf Of shiplu > Sent: Tuesday, January 26, 2010 4:25 PM > To: Daniel Brown > Cc: Daevid Vincent; PHP-General > Subject: Re: [PHP] If the first four characters are "", > 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 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.1975422 [preg_match] => 1.25615906715 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {} - timing tests
Ran this code (http://pastie.org/795983) The result is, array of 10,000 Array ( [[]] => 5.66168689728 [strpos] => 5.70796895027 [substr] => 5.92751288414 [preg_match] => 6.21515512466 ) -- Shiplu Mokaddim My talks, http://talk.cmyweb.net Follow me, http://twitter.com/shiplu SUST Programmers, http://groups.google.com/group/p2psust Innovation distinguishes bet ... ... (ask Steve Jobs the rest) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {} - timing tests
On Tue, Jan 26, 2010 at 17:17, Daevid Vincent wrote: > > 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... > [snip=code] > > 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... Right. I removed the note you posted to that effect a few moments ago because you didn't explain what was being done. If you'll resubmit it with a snippet of the conversation (or even just, "in discussions about benchmarks with this vs. that vs. those, blah, blah, blah") it'll stay up. It's a good test result, and worthwhile for those building large-scale systems. Kudos to you, Doctor! ;-P >> Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P > > By the way, I loved your book "Da Vinci Code" ;-p I was always too busy writing it to read it. -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] If the first four characters are "0000", then do {} - timing tests
> -Original Message- > From: paras...@gmail.com [mailto:paras...@gmail.com] On > Behalf Of Daniel Brown > Sent: Tuesday, January 26, 2010 8:21 AM > To: Daevid Vincent > Cc: John Taylor-Johnston; PHP-General > Subject: Re: [PHP] If the first four characters are "", then do {} > > On Mon, Jan 25, 2010 at 22:51, Daevid Vincent > wrote: > >> -Original Message- > >> From: paras...@gmail.com [mailto:paras...@gmail.com] On > >> Behalf Of Daniel Brown > >> Sent: Monday, January 25, 2010 6:43 PM > >> To: John Taylor-Johnston > >> Cc: PHP-General > >> Subject: Re: [PHP] If the first four characters are > "", then do {} > >> > >> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston > >> wrote: > >> > I am reading the manual: > http://ca.php.net/manual/en/ref.strings.php > >> > > >> > $mydata->restored = "-00-00"; > >> > >> >> > >> $o[] = '0942-23-23'; > >> $o[] = '-00-00'; > >> $o[] = '1238-00-00'; > >> $o[] = '0001-23-45'; > >> $o[] = '-11-22'; > >> > >> for($i=0;$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) == "") { } > > Overkill? > > > $o[] = '0942-23-23'; > $o[] = '-00-00'; > $o[] = '1238-00-00'; > $o[] = '0001-23-45'; > $o[] = '-11-22'; > > $now = microtime(); > > for($i=0;$iif(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 if(substr($o[$i],0,4) == "") { > //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... 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.1975422 [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
Re: [PHP] If the first four characters are "0000", then do {}
On Mon, Jan 25, 2010 at 22:51, Daevid Vincent wrote: >> -Original Message- >> From: paras...@gmail.com [mailto:paras...@gmail.com] On >> Behalf Of Daniel Brown >> Sent: Monday, January 25, 2010 6:43 PM >> To: John Taylor-Johnston >> Cc: PHP-General >> Subject: Re: [PHP] If the first four characters are "", then do {} >> >> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston >> wrote: >> > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php >> > >> > $mydata->restored = "-00-00"; >> >> > >> $o[] = '0942-23-23'; >> $o[] = '-00-00'; >> $o[] = '1238-00-00'; >> $o[] = '0001-23-45'; >> $o[] = '-11-22'; >> >> for($i=0;$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) == "") { } Overkill? 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(). Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
maybe another way can be $str = explode('-',$mydata->restored); if($str[0] == '') { dostuff();} ;) On 1/26/10, shiplu wrote: > Another technique could be. > > $s = $mydata->restored; > > if($s[0]==$s[1] > && $s[1]==$s[2] > && $s[2]==$s[3] > && $s[3]==0){ > > do_work(); > } > > > But I think you got the best solution already. > > -- > Shiplu Mokaddim > My talks, http://talk.cmyweb.net > Follow me, http://twitter.com/shiplu > SUST Programmers, http://groups.google.com/group/p2psust > Innovation distinguishes bet ... ... (ask Steve Jobs the rest) > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
Another technique could be. $s = $mydata->restored; if($s[0]==$s[1] && $s[1]==$s[2] && $s[2]==$s[3] && $s[3]==0){ do_work(); } But I think you got the best solution already. -- Shiplu Mokaddim My talks, http://talk.cmyweb.net Follow me, http://twitter.com/shiplu SUST Programmers, http://groups.google.com/group/p2psust Innovation distinguishes bet ... ... (ask Steve Jobs the rest) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
On Mon, Jan 25, 2010 at 08:12:43PM -0800, Daevid Vincent wrote: > > -Original Message- > > From: Paul M Foster [mailto:pa...@quillandmouse.com] > > Sent: Monday, January 25, 2010 8:05 PM > > To: php-general@lists.php.net > > Subject: Re: [PHP] If the first four characters are "", then do {} > > > > On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote: > > > > > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php > > > > > > $mydata->restored = "-00-00"; > > > > > > How do I express this? If the first four characters are > > "", then do {} > > > > > > What I am looking for is in strpos(), no? > > > > > > if () > > > { > > > } > > > > >From what I understand, strpos() faster than a lot of other similar > > string functions and much faster than regexps. You could do: > > > > if (strpos($mydata->restored, '') === 0) { > > do_stuff(); > > } > > Ah. Clever use of the "== 0" (tripple equals not necessary). No, the === was purposeful, since strpos can return false if there's no match. If you simply use == 0, it will trigger on both false, and the 0 index. If you use !== false, you're only testing for no match. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
On Mon, Jan 25, 2010 at 11:12 PM, Daevid Vincent wrote: >> >From what I understand, strpos() faster than a lot of other similar >> string functions and much faster than regexps. You could do: >> >> if (strpos($mydata->restored, '') === 0) { >> do_stuff(); >> } > > Ah. Clever use of the "== 0" (tripple equals not necessary). > The OP needs to know if it's the first 4 or not and so position 0 would be > the start. Very nice. > The triple equals IS necessary. If '' is not found at all in $mydata->restored, strpos() will return false, and (false == 0) in PHP. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] If the first four characters are "0000", then do {}
> -Original Message- > From: Paul M Foster [mailto:pa...@quillandmouse.com] > Sent: Monday, January 25, 2010 8:05 PM > To: php-general@lists.php.net > Subject: Re: [PHP] If the first four characters are "", then do {} > > On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote: > > > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php > > > > $mydata->restored = "-00-00"; > > > > How do I express this? If the first four characters are > "", then do {} > > > > What I am looking for is in strpos(), no? > > > > if () > > { > > } > > >From what I understand, strpos() faster than a lot of other similar > string functions and much faster than regexps. You could do: > > if (strpos($mydata->restored, '') === 0) { > do_stuff(); > } Ah. Clever use of the "== 0" (tripple equals not necessary). The OP needs to know if it's the first 4 or not and so position 0 would be the start. Very nice. Now, if his data is always of the format -XX-XX as it was illustrated, then this will also work if (strpos($mydata->restored, '-') !== false) { do stuff; } (note the hyphen) and you should check the === false -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote: > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php > > $mydata->restored = "-00-00"; > > How do I express this? If the first four characters are "", then do {} > > What I am looking for is in strpos(), no? > > if () > { > } >From what I understand, strpos() faster than a lot of other similar string functions and much faster than regexps. You could do: if (strpos($mydata->restored, '') === 0) { do_stuff(); } Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] If the first four characters are "0000", then do {}
> -Original Message- > From: paras...@gmail.com [mailto:paras...@gmail.com] On > Behalf Of Daniel Brown > Sent: Monday, January 25, 2010 6:43 PM > To: John Taylor-Johnston > Cc: PHP-General > Subject: Re: [PHP] If the first four characters are "", then do {} > > On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston > wrote: > > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php > > > > $mydata->restored = "-00-00"; > > > $o[] = '0942-23-23'; > $o[] = '-00-00'; > $o[] = '1238-00-00'; > $o[] = '0001-23-45'; > $o[] = '-11-22'; > > for($i=0;$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) == "") { } Or in your very specific case you could do the harder way and note that strings work like simple arrays too in a way, so $mydata->restored{0} through $mydata->restored{3} should all be '0' ( note the {} and not [] ). You can use a forloop or just manually check them all with && or || or whatever logic you feel comfortable with. But the substr is by far the simplest. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
- Original Message - From: "John Taylor-Johnston" To: "PHP-General" Sent: Tuesday, January 26, 2010 12:36 PM Subject: [PHP] If the first four characters are "", then do {} I am reading the manual: http://ca.php.net/manual/en/ref.strings.php $mydata->restored = "-00-00"; How do I express this? If the first four characters are "", then do {} What I am looking for is in strpos(), no? if () { } if (substr($mydata,0,4) == ""{ Do stuff } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] If the first four characters are "0000", then do {}
On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston wrote: > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php > > $mydata->restored = "-00-00"; -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] If the first four characters are "0000", then do {}
I am reading the manual: http://ca.php.net/manual/en/ref.strings.php $mydata->restored = "-00-00"; How do I express this? If the first four characters are "", then do {} What I am looking for is in strpos(), no? if () { } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php