Re: Hidden costs of PHP arrays?

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

 



On Wed, 28 Jan 2009 00:50:18 +0000, nrixham@xxxxxxxxx (Nathan Rixham) wrote:

>Clancy wrote:
>> Also what the relative virtues of defining the same set of fields for every contact, as
>> against either defining only the fields which actually hold values, as in the following
>> examples?
>> 
>> a:
>> $contacts['clancy']['home_address'] = 'jkjkjk';
>> $contacts['clancy']['home_phone'] = 0123 4567;
>> $contacts['clancy'][' office_address''] = '';
>> $contacts['clancy']['office_phone'] = '';
>> $contacts['joe']['home_address'] = '';
>> $contacts['joe']['home_phone'] = '';
>> $contacts['joe']['office_address'] = 'jsfvkl';
>> $contacts['joe']['office_phone'] = 'jsfvkl';
>> 
>> b;
>> $contacts['clancy']['home_phone'] = 0123 4567;
>> $contacts['clancy']['home_address'] = 'jkjkjk';
>> $contacts['joe']['office_address'] = 'jsfvkl';
>> $contacts['joe']['office_phone'] = 'jsfvkl';
>> 
>
Thanks to everyone who has commented. 

>if you go for option b; you're going to have do a vast amount of isset() 
>checks in a for loop and all kinds of fancy business logic for
>
>$contacts['clancy']['home_phone'] = 0123 4567;
>vs
>$contacts['clancy']['home_phone'] = '';
>vs
>'home_phone' not set
>

This question is far less clear-cut than you might assume. The data is actually stored as
a text document, with a separate line for each piece of information. To minimise the file
length blank fields are simply omitted. When I load the file into memory it is much
simpler to enter only the fields which are actually specified. As there are ~600 entries
this presumably gives a significant saving in loading time and in memory.

However if I take this approach I then have to add an "if(isset( ...))" test before I read
each field when I'm trying to access a specific entry. This would only increase the memory
usage by a very small amount, and would have a negligible effect on timing.

So if I fill in all the entries when I load the file it will increase the loading times
slightly, and use slightly more memory.  On the other hand it will save me from having to
think about it whenever I access a variable.  The question really comes down to whether
the saving in memory if I don't load the empty variables is worth the extra programming
hassle when I write new procedures for accessing an entry.

>so one would guess that code would far outweigh any tiny speed gain from 
>dropping the additional items in the array.
>
>on another note; php has been stable and speedy now for a very long 
>time, some bit's like the reflection api could be speeded up a little 
>bit as demand grows, but certainly all scalars and compound types have 
>been tested and optimized to hell and back (afaik).
>
One could reasonably hope that the same could be said for every part of the programming
chain, but it is one of the ironies of modern computing that computers get faster and
faster, memory gets cheaper and cheaper, programming appears to get simpler and simpler,
yet the programs get slower and slower. I have a fairly modern (maybe 2yo) computer, and
use XP professional 5.1. Not long ago I switched to Office 2007 12.0, and when I did so I
was appalled to discover that if I had a document loaded into Word, and hit Ctrl A, I
could watch the highlighting scroll down the screen!

As a former assembly language programmer I have some idea of the vast amount of thumb
twiddling which is going on behind-the-scenes when I make some apparently simple request
like the one to get my phone number. Undoubtedly most of this occurs in the murky depths
of the operating system, but if there were any simple way to avoid adding to it
unnecessarily it would be nice to know about it.

>you can test this all you're self, simply populate an array with say 
>1000 random other arrays of data with 10 keys each, stick it in a for 
>loop and time several times, then do the same for an array as above but 
>with subarrays of only 5 keys; see if you can spot any significant 
>difference. [then compare to say a single database select, or an 
>execution of a small script.]
>
>another way of putting it; I'm 99% sure that nobodies code is perfectly 
>optimised enough by itself to notice any performance hits from php; 
>quite sure you could make far bigger gains by optimising you're own code 
>first :p
>
>regards! <ramble>

-- 
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