|
Posted by Michael Martinek on 11/26/07 12:55
> So as far as I can tell: it is not a waste of memory, but it offers you
> the choice what you want:
> 1) faster code/less IO (use only indexes)
> 2) better readable/more robust code. (use assoc arrays)
>
> just my 2 cent.
>
> Regards,
> Erwin Moller
And I fully agree with you, Erwin.
As for the general topic, if you want to be able to expand on your
code later, without having to worry about reviewing every SQL query..
use associative arrays. For example, if you have a SQL query 'SELECT *
FROM table WHERE something'.. then your numerical indexed array is
going to be specific. But what happens if a DB admin inserts a new
column somewhere, or drops one? Now you'll need to review all the
code.. and good luck with that, because $row[3] is harder to find
meaning in than $row['data_crc']. And seriously, we're in a day and
age where using a little extra memory here or there isn't going to do
any harm. In a business scenario, it'll likely cost you more to be as
system efficient as you possibly can.
I always use mysql_fetch_assoc, unless I'm doing a "SELECT
COUNT(*) ..."
If it's a script you know is going be extremely intensive to run, and
using some extra storage here and there is going to bog down resources
then use fetch_array.. but if that's the case, you might want to
upgrade. ;) Also keep in mind, signed integers aren't free.. and if
you're really worried about resources, do an unset() on variables when
they are large arrays and contain many entries. When passing arrays to
a function, pass them by reference so a new copy isn't made.
My 1/50th of a dollar in summation:
If you're writing code for a business, contract work, or something
that will need to be maintained then use associative arrays. Otherwise
if you're writing a quick one-time usage script that probably won't
need to be maintained, expanded on, debugged.. then go ahead and used
numerically indexed arrays. However, the speed increase over
associative arrays in normal circumstances is not worth using as a
reason to do so.
[Back to original message]
|