I’m working on something that needs to be pretty efficient in terms of memory usage, and I was hoping someone here knows… Is it more efficient to store data in arrays with an integer referencing another array of strings, or the other way around? Each string referencing the integers.
An example would be storing football teams jersey numbers… Let’s say there are jersey numbers 1 through 20, and about 200 different football teams – each football team will have jersey numbers 1 through 20, and each jersey number will be in each football team. Would it be better to store this data like:
JerseyNumber(Int)=>array(team, team, team, etc.)
or
TeamName(String)=>array(1,2,3,4,etc.)
Does it make a difference? Either way the data will be stored in a cache, so it’s not like it’s a matter of CPU usage – just a matter of memory usage.
I’ve included the code I’m using to generate the test data… I had attempted to use memory_get_usage() (Before and after, getting the difference), but I’m not sure how to accurately get the actual memory usage of an array this way.
$ids = array();
for ($i = 0; $i < 500; $i++) {
$ids[] = $i;
}
$tmpArray = array();
$values = array();
for ($i = 0; $i < 60; $i++) {
$randString = randString(rand(60,100));
$values[] = $randString;
}
foreach ($values as $feValues) {
$tmpArray[$feValues] = $ids;
}
$tmpArrayTwo = array();
foreach ($ids as $feIds) {
$tmpArrayTwo[$feIds] = $values;
}
Materially, Nope. You won’t be able easily to examine the internals of this, as the PHP storage engine is quite complex and this level is in the noise. Even for large complex structures, it is difficult to track storage usage as PHP uses a reference based copy-on-write. This sort of post starts to give you an inkling of what goes on under the hood. 🙂