On Sun, 2002-02-03 at 14:06, Lars Torben Wilson wrote:
> On Sun, 2002-02-03 at 12:12, Mike Frazer wrote:
> > Your reply piqued my curiosity so I whipped together a script that times the
> > execution of a foreach(), a while() and a for() loop executing the same
> > commands. The results were rather surprising in some ways but not really
> > overall.
> >
> > The while() loop is, as you stated, is quicker than a foreach() loop.
> > However, the for() loop is the fastest of all three. Below are the results
> > of three tests:
>
> Odd.. I get the opposite result:
Oh for Pete's sake. Actually, if you look at my code below and find the
error, you'll see that foreach() actually wins, followed by while() and
then for(). My mistake.
Torben
> Running test_foreach() 5000 times...2.6176570653915 seconds
> Running test_for() 5000 times...3.0155370235443 seconds
> Running test_while() 5000 times...2.0656290054321 seconds
>
> Results:
> test_foreach_1: 2.6176570653915
> test_for_2: 3.0155370235443
> test_while_3: 2.0656290054321
>
> Using the following code:
>
> <?php
> error_reporting(E_ALL);
>
> function test_while() {
> global $upload_file;
> reset($upload_file);
> foreach ($upload_file as $line) {
> $num_pipes = substr_count($line, '|');
> if ($num_pipes < 2 || $num_pipes > 4) {
> return false;
> }
> }
> return true;
> }
>
>
> function test_foreach() {
> global $upload_file;
> reset($upload_file);
> while (list(, $line) = each($upload_file)) {
> $num_pipes = substr_count($line, '|');
> if ($num_pipes < 2 || $num_pipes > 4) {
> return false;
> }
> }
> return true;
> }
>
>
> function test_for() {
> global $upload_file;
> for ($i = 0; $i < count($upload_file); $i++) {
> $num_pipes = substr_count($upload_file[$i], '|');
> if ($num_pipes < 2 || $num_pipes > 4) {
> return false;
> }
> }
> return true;
> }
>
> $filename = 'testpipes.txt';
> $upload_file = file($filename);
>
> speedtest(array('test_foreach',
> 'test_for',
> 'test_while'), 5000);
>
> ?>
>
>
> Curioser and curiouser....
>
>
> Torben
>
> --
> Torben Wilson <[EMAIL PROTECTED]>
> http://www.thebuttlesschaps.com
> http://www.hybrid17.com
> http://www.inflatableeye.com
> +1.604.709.0506
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
Torben Wilson <[EMAIL PROTECTED]>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php