Re: Wierd ass code...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14/05/06, Gonzalo Monzón <gmc@xxxxxxxxxxxxx> wrote:
Satyam escribió:
> Of course, another possibility is that the author does not know how to
> handle multidimensional arrays (just to say it before anyone else
> points an obvious alternative)
>

I don't think the author does not know how to handle multidimensional
arrays. I thought the author was more concerned about performance than
other issues people like as coding "beautifuly"...  Maybe author's
approach is weird, but its twice faster in the worst case!!

Sure the author only needs to loop the whole array and doesn't need
multi-dimensional approach... 'cause its a lot faster to iterate once
the array is filled!!

Look at the test I've done (below) its seems filling multi-dimensional
arrays is n/2 faster than single dimension, but then when you need to
read the array data, a multi-dimensional approach is always worse than
n*6!!

Your setup is odd. I get much more similar results with both php 4 and 5.

You're also using a more than slightly odd way of iterating through the array.

These does exactly the same as your Test functions and are considerably faster:

<?php
function Test_c() {
 global $testC;
 for ($a=1;$a<999;++$a) {
   for ($b=1;$b<999;++$b) {
     $testC[$a][$b] = $a . $b;
   }
 }
}

function Test_c2() {
 global $testC;
 foreach ($testC as $k => $v) {
   foreach ($v as $k2 => $v2);
 }
}
?>

Test results from PHP 5.0.5:

Fill style A, bidimensional array: 3.0747
Iteration style A, bidimensional array: 2.4385
Total time style A: 5.5135

Fill style B, single-dimension array: 4.6502
Iteration style B, single-dimension array: 2.3964
Total time style B: 7.047

Fill style C, bidimensional array: 2.5237
Iteration style C, bidimensional array, foreach loop: 1.3415
Total time style C: 3.8656

Test results from PHP 4.4.0:

Fill style A, bidimensional array: 3.2056
Iteration style A, bidimensional array: 2.3848
Total time style A: 5.5907

Fill style B, single-dimension array: 5.1468
Iteration style B, single-dimension array: 2.4016
Total time style B: 7.5488

Fill style C, bidimensional array: 2.6899
Iteration style C, bidimensional array, foreach loop: 1.4073
Total time style C: 4.0976

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux