Re: Why is the php loop, 'for()', so slow????

From: Date: Thu, 20 Nov 2003 00:27:07 +0000
Subject: Re: Why is the php loop, 'for()', so slow????
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-170363@lists.php.net to get a copy of this message
On Wed, Nov 19, 2003 at 04:54:39PM -0500, Scott Fletcher wrote: : : "Ray" <ray.list@powerweb.net> wrote: : > try : > $res_str_len = strlen($res_str); : > for ($i=1;$i<$res_str_len;++$i) : > : > one less function call in the loop and the prefix version of ++ is : > sometimes a tad faster then the postfix version. : : That's a great idea! Recently changed the code and the postfix of ++ to : prefix of ++. Still about the same... : : What so odd about this script is that I wrote it in Visual Basic for one of : the application. It took only a few seconds for a PC workstation (Pentium : 3 - Windows 2000). So, converting this code to PHP and place it on the Unix : Server (RS/6000) Webserver and it take way too long.... I tried it myself. The old code that calculated strlen() on each iteration took around 8.2 seconds. The new code that ran strlen() once and stored the results in a variable took around 5.6 seconds. However, the big problem with your code is that you manually iterate through every character of the string, then do your search for both "<![CDATA[" and "]]>". That's not an efficient search algorithm at all. Take a look at strpos() to get at least 1-2 orders of magnitude of speedup.

« previous php.general (#170363) next »