Re: Wierd ass code...
| From: | Gonzalo Monzón | Date: | Mon, 15 May 2006 12:48:23 +0000 |
| Subject: | Re: Wierd ass code... | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-236194@lists.php.net to get a copy of this message | ||
Robin Vickery escribió:
On 14/05/06, Gonzalo Monzón <gmc@serveisw3.net> wrote:Don't thought my setup is odd. Only I can say my computer maybe isn't as faster than yours. I use centrino mobile, windows xp, and test was run on php 4.3.9 ( sorry i missed that, thought was 4.3.4) I know it can be coded to be faster, but my point with that php snippet was to compare the same "odd" way with single or multi dimensional approaches. Sure $a.$b is faster than "$a$b" and foreach than do while (the more if you didn't "use" each element datum!! that's hi - optimization! does sense for you using foreach without assigning any value from it? yes, you've got them inside foreach $v2 - $k2, but in my code i've got too them inside key($v) $v[key($v)], and I do assign them to another var, so if you want to compare performance, please to it too! anyway I checked this and both multidimensional approaches performs pretty equal. ) I didn't code that snippet to the best / faster execution methodology for this individual case but a generic "odd" way for comparing both cases. The same I did in python, as that language as far more adaptable data structures for letting you choose the best for your case needs (I could use python numarray (uses C arrays) and I thought you could't beat it in PHP or any PHP C extension, but that's far from the purpose of my posts. If do I use $a.$b instead "$a$b" and assign each element key & val from foreach in "type c" test, that's what I got: -see below- B & C perform nearly equal on total time, of course, the best approach would be highly dependent on the real data to use. The only I thing could say about your foreach approach is I allways avoided using it in some kind of projects, as it does a copy of the data on every iteration, so when using lots of objects, huge data, or pass-per-reference had lot of memory performance issues using it in php4 -Im speaking about very long-running scripts where these issues really care- so do { } while(next()) is the only you can use safely... Notice huge gap in running the type A test in my computer and in yours for php 4.4.0 - me 4.3.9, you get 5.59 and I got 13.9, and other tests seems my install always performs better than yours! :-) (type b: you 5.12 - 2.40 = 7.54 me 4.08 - 1.66 = 5.57 that's about 2 secods!!). That means to me so this test don't probe nothing, they are all very variable... I don't know why that great difference, but I don't think the php revision changes from 4.3.9 to 4.4.0 are relevant enought as for these performance gaps. We should use a real tests library for choosing best case of several runs, and so on... Anyway these are the results using your test_c modified code with two lines added... notice filling type A and C share the same code, and there is some gap: 1,97 - 2,16 ! Fill style A, bidimensional array: 1,9726 Iteration style A, bidimensional array: 11,9345 Total time style A: 13,9319 Fill style B, single-dimension array: 4,0895 Iteration style B, single-dimension array: 1,664 Total time style B: 5,7598 Fill style C, bi-dimensional array: 2,1671 Iteration style C, bi-dimensional array: 3,6194 Total time style C: 5,7958 Regards, Gonzalo function Test_c2() { global $testC; foreach ($testC as $k => $v) { foreach ($v as $k2 => $v2) {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 Hi Robin,
->> $z = $k2; ->> $y = $v2;} } }