patternphpMinor
What is the most memory efficient way to load big arrays into PHP script?
Viewed 0 times
scriptthearrayswhatintophpefficientwaybigmemory
Problem
I have a few big arrays stored in separate files that are included into PHP as and when they are required:
Array File:
Main Script:
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?
Array File:
Main Script:
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?
Solution
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.
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.
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 ...
}Code Snippets
<?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 ...
}Context
StackExchange Code Review Q#25330, answer score: 2
Revisions (0)
No revisions yet.