Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a few big arrays stored in separate files that are included into PHP as and when they are required:

Array File:

<?php
 $cty_arr["af"] = "Afghanistan";
 $cty_arr["ax"] = "Åland Islands";
 $cty_arr["al"] = "Albania";
 $cty_arr["dz"] = "Algeria";
 $cty_arr["as"] = "American Samoa";
...
?>

Main Script:

<?php
 require("/var/www/include/array_file.php");
...
?>

I am not very sure how PHP utilizes memory, but I have this thought that some RAM is allocated to store the big array for each instance that the script is run. Since these arrays are static, could there be a better way of loading such arrays to achieve some memory savings?

share|improve this question
    
Slightly related to PHP parse_ini_file() performance? –  mnhg Apr 22 '13 at 9:53
    
I think you should read about Memcache and similar concepts. In plain php, I think there is nothing you can do about it. –  mnhg Apr 22 '13 at 9:56

1 Answer 1

I thought about this again. My comments are still valid, so including it in a php file is the cheapest way to get the array into your system and without a shared memory you will not reduce the total memory usage.

But maybe you can optimize it at an higher level of abstraction. Assuming that you always only need some countries/settings/localization in your loaded page and never all, you could split this array into multiple files.

<?php
...
function countryCodeToName($key)
{
    if (isset($this->countries[$key])) return $this->countries[$key];
    $firstCharacter=$key[0]
    include "country_".$firstCharacter.".php"; //similar to your file above
    $this->countries=$this->countries + $cty_arr;
    //some handling if still missing 
    //and return ...
}
share|improve this answer
    
+1 ,this is quite an interesting approach.. Let me spend some time to think about it. Thanks :) –  Question Overflow Apr 22 '13 at 14:36
    
@QuestionOverflow Please post your benchmark here again. I'm really interested in the results. –  mnhg Apr 23 '13 at 5:22
    
I have thought over it and finds that this solution is not really practical since there will be some scripts where I would need to echo all the values from the array, then there would be others where a portion of it will be selected at random. I have also did a little benchmark with memory_get_usage() both true and false, to get the memory allocated and used by the script. In one of the scripts, the memory allocated doubles from 262kB to 524kB as soon as the array is loaded although the memory used only increased by 15%. Do you know of any other way to do the benchmark? Thanks. –  Question Overflow Apr 25 '13 at 6:07
    
I think for a benchmark memory_get_usage(false) is the better start. You will see even minor changes. But finally the real memory is import and even if you only need one Byte of a new block all the memory will be listed when you call the method with true. –  mnhg Apr 25 '13 at 7:55
    
So if you don't find a way to use only a part of the values, I think you have do check if you can use a Memcache etc. –  mnhg Apr 25 '13 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.