Forum Moderators: coopster

Message Too Old, No Replies

optimising code for speed vs. resources

with regard to arrays

         

jamie

12:20 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

i am interested in scalability as opposed to outright speed as our site can get quite busy at times.

with this in mind, which would be better coding practice? to use the native array_push() and array_walk() functions, or to use while() and for() loops, etc.

Example 1

while ($row = mysql_fetch_array($result)) {
$my_array[] = $row;
}

or

while ($row = mysql_fetch_array($result)) {
array_push($my_array, $row);
}

do the different ways of populating the array above have any difference in speed / resource optimisation?

Example 2

function my_function() {
.. define function
}
array_walk($my_array, 'my_function');

or

$a = sizeof($my_array);

for ($x = 0; $x < $a; $x++) {
...do stuff to $my_array[$x]
}

in the above example i assume that to declare a function and then array_walk() it would be a lot slower than using a for() loop.

aren't functions to be avoided unless it is code which is repeated more than twice - aren't they a real resource-eater?

cheers

Xuefer

3:53 pm on Jun 26, 2003 (gmt 0)

10+ Year Member



i haven't tested
but theoretically, i prefer: $array[] = $element;
or even: $array[] = &$element;
because function call is expenses(which u might care)
and reference is faster than copy a large object/array(that's why php5 use reference for object by default)
but, in fat, someone complain in bugs.php.net that reference is even slower than copy
it's better to test it, for different php version, may result different speed

and in both your example, i don't see any resource(except cpu resource) is different.

jamie

4:36 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi xuefer thanks for feedback.

it is exactly the cpu resources and function calls which i am worried about.

i have not yet used references like $array[] = &$element; so will look into that. cheers.

daisho

10:11 pm on Jun 26, 2003 (gmt 0)

10+ Year Member



To be honest I use to think that way. After someone else here in the forums tested what I assumed it turns out there is very little difference. Not I don't worry about little differences that much. You will be much better of using some type of PHP Optimizer.

daisho.

Nick_W

10:18 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamie, that's an excellent question. Thanks!

Nick

dingman

10:26 pm on Jun 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and reference is faster than copy

Doesn't PHP have copy-on-write semantics, though?

Xuefer

2:22 am on Jun 27, 2003 (gmt 0)

10+ Year Member



sorry, i didn't know copy-on-write before :P

even no copy-on-write, and reference faster than copy
we still have to test it
because it's a scripting language
we write 1 statement, the engine may need many steps to finish it, and we don't/needn't know how the engine works exactly

jamie

6:55 pm on Jun 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



what do you pros recommend as an optimizer?

there's a lovely article with excellent links for further reading here [php.weblogs.com] which mentions (hope the URL is ok jatar_k)

zend
afterburner
PHPA
APC

i must say i am tempted to get the zend optimizer + accelerator (costs-money-must-be-good way of thinking but also because of the reliability ;-)

i was in quite a few forums and mailing lists for the other products in which server crashes and malfunctioning code were an issue.

would very much appreciate some real-life experiences.

cheers

p.s. thanks nick :-)

daisho

7:59 pm on Jun 27, 2003 (gmt 0)

10+ Year Member



PHPA has very good ratings. I use it on my site and it works quite well as far as I can tell. To be honest when I did my research PHPA seems to be the best free solution. I never compared passed that.

daisho.

jatar_k

8:18 pm on Jun 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have used zend and thought it was stellar. Though it is the only one I have actually used I have also heard good things about phpa.