RE: [PHP] Why is the php loop, 'for()', so slow????

From: Wouter Van Vliet (wouter_at_springt.nl)
Date: 11/20/03


To: "Scott Fletcher" <scott@abcoa.com>, <php-general@lists.php.net>
Date: Thu, 20 Nov 2003 16:01:41 +0100

As a substitute for substr, you might want to give a regex a chance.. have
no clue if it's faster, but it might just be.

        /^.{2}(.{0,8})/

would be your regex if you want to start at offset THREE and take out a MAX
EIGHT char string (remove the 0 and get ONLY EIGHT char strings). Not sure
if PHP sets $1, $2, .. vars, else give the optional third "Matches" param to
preg_match();

Wouter

> -----Oorspronkelijk bericht-----
> Van: Scott Fletcher [mailto:scott@abcoa.com]
> Verzonden: donderdag 20 november 2003 15:44
> Aan: php-general@lists.php.net
> Onderwerp: Re: [PHP] Why is the php loop, 'for()', so slow????
>
>
> Well, should have make one long string to the $res_str variable a lot
> shorter. :-)
>
> It turned out that the for() loop isn't the slow part when you mentioned
> about substr(). I tried out the while() loop and it is pretty
> much the same
> when the loop take over 5 minutes. So, it now seem to have to do with
> substr() function. Yea, I'm not sure what hte best substitute of it would
> be. In other branches off of this posting, someone said about using the
> strpos(). I'm willing to give this a try but I have problem with this
> because I have two "<!CDATA[[***]]>" tags in it and I want to use
> both, not
> just first one.
>
> Scott
>
> "Chris W. Parker" <cparker@swatgear.com> wrote in message
> news:001BD19C96E6E64E8750D72C2EA0ECEE4477E3@ati-ex-01.ati.local...
> Scott Fletcher <mailto:scott@abcoa.com>
> on Wednesday, November 19, 2003 1:12 PM said:
>
> > function CBC_XML_BreakUp(&$strResponse_XML, &$strResponse_HTML)
> >
> > {
>
> [snip]
>
> Wow I didn't think you were going to post your whole program. :0
>
> 1. Are you sure the for() loop is the slow part?
>
> 2. As someone already suggested, calculating the sizeof() outside of the
> loop should help a lot. Another enhancement is changing your for() to
> while(). (This is a small enhancement but makes a bigger difference as
> your iterations increase.)
>
> REGULAR for() loop construct:
>
> $iMax = 99;
>
> for($iCnt = 0; $iCnt < $iMax; $iCnt++)
> {
> }
>
> OPTIMIZED:
>
> $iMax = 99;
> $iCnt = -1;
>
> while(++$iCnt < $iMax)
> {
> }
>
> Like I said it's only slightly faster, but might make a difference
> depending on your number of iterations.
>
> 3. I think what may be slowing you down is your substr() calls. Maybe
> there is a substitute function that is faster? (I don't have any ideas
> unfortunately.)
>
>
> Let us know if you figure something out.
>
> HTH,
> Chris.
> --
> Don't like reformatting your Outlook replies? Now there's relief!
> http://home.in.tum.de/~jain/software/outlook-quotefix/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php



Relevant Pages

  • Re: Using foreach loop to create radiobutton menu
    ... Your foreach loop is not the problem, the problem is how you define the -command. ... The only possible thing the interpreter can do is substitute the current value of range, which is likely the last value once your loop exited. ... One choice is double quotes. ... The double quotes means that $range gets expanded while in the loop, long before the puts command actually runs. ...
    (comp.lang.tcl)
  • Re: formula to count charcter in a range
    ... you use of the word "effectively" is meant to cover the fact that, for example, in "*w*w*", once one 'w' is found, the search loop must continue on in order to look for the next 'w'. ... Your formula would iterate through range as many times as there are ... argument contains wildcards isn't as simple as my SUBSTITUTE call. ... in range, A were the number of sequential entries in instance_array, ...
    (microsoft.public.excel.worksheet.functions)
  • Re: formula to count charcter in a range
    ... Your formula would iterate through range as many times as there are ... argument contains wildcards isn't as simple as my SUBSTITUTE call. ... EACH entry in range, approximating an inner loop. ... in range, A were the number of sequential entries in instance_array, ...
    (microsoft.public.excel.worksheet.functions)
  • do loop with msg box
    ... Looking for a "neater coding" method using a Do Loop ... to substitute and clean up code that already works. ... The user clicks a command button to hide the subform. ...
    (microsoft.public.access.formscoding)
  • Re: [PHP] Why is the php loop, for(), so slow????
    ... It turned out that the forloop isn't the slow part when you mentioned ... substr() function. ... Like I said it's only slightly faster, ... depending on your number of iterations. ...
    (php.general)