Reputation: 1842
Suppose I have a string... 'iAmSomeRandomString'
. Let's say I choose to store this string in two ways:
$stringVar = 'iAmSomeRandomString';
$stringArr = array('rando' => 'iAmSomeRandomString');
If I later would like to access that string, is using $stringVar
or $stringArr['rando']
more performant?
My understanding is that there was no performance difference, but I was asked this in an interview and told I was wrong. So now I want to know what's up.
Upvotes: 0
Views: 70
Reputation: 72177
It's obvious that the first case (keeping the values in individual variables) uses less memory and requires less time for access.
An array is a variable itself and, besides the memory needed to keep its content, it also uses memory for its bookkeeping. The items stored in an array are kept in linked lists to allow sequential access (foreach
) and there is also a map that allows direct access to elements ($array[$index]).
All these internal data structures use memory and they grow as the array grows (the memory overhead of the array is not constant).
Regarding the time needed to access the data, an array introduces a supplemental level of indirection. Accessing a simple string variable requires finding its name in the current table of symbols then its content is accessible. Using an array, the operations happen twice: find the array variable and its content then find the desired value inside the array.
However, for most scripts, the difference on both memory consumption and access time is not significant and it is not worth trying to optimize anything this way.
Upvotes: 1
Reputation: 70853
Is there a relevant performance difference? No.
...
Output for 7.0.4
StartiAmSomeRandomStringiAmSomeRandomString
First case: 0.000003
Second case: 0.000005
Output for 7.0.3
StartiAmSomeRandomStringiAmSomeRandomString
First case: 0.000023
Second case: 0.000015
...
Note that you see numbers that vary between versions, but highly depend on the basis of where they are run. You probably cannot compare two versions.
Also note that I started the text output before measuring - this is because without it, you would measure the performance penalty of PHP preparing for output, and this is a relevant effect in this test.
Your question is related to an interview situation. While it is always easier to tell afterwards what would be a better answer, I'd still like to stress that it should not matter in which way variables are stored. The more important factor should be if the code you write is readable and if the code you read is understood.
It simply does not make sense to store a value into an array because of performance reasons if the value is guaranteed to be a single value. Storing it into an array tells me that there will be more values. If I don't immediately see where they are added, I will start searching for them, because if I need to change something in the array, I don't want to use a key that is used somewhere else.
Also, the question targets simple global variables. I would think that a proper application design will use objects. Why didn't they ask about class properties? Why didn't they ask about static vs. dynamic properties and their performance difference, or public vs. protected vs. private variables?
A good candidate probably would discuss the usefulness of the question itself, and challenge the source of the performance data that decides the correct answer. If you change the code in the above link and remove the first echo statement, you will see that the data changes dramatically.
Output for 5.5.2, 5.5.25, 5.6.10, 7.0.4
iAmSomeRandomStringiAmSomeRandomString
First case: 0.000031
Second case: 0.000004
Output for 7.0.3
iAmSomeRandomStringiAmSomeRandomString
First case: 0.000127
Second case: 0.000017
Upvotes: 1
Reputation: 4244
First case will consume less memory and will be faster. But the difference is so tiny that it doesn't really matter most of the time. Though, yes, technically they're right.
$time_start=microtime(true);
for ($i=0; $i < 100000; $i++)
{
$stringVar=1;
$stringVar++;
}
$time_end=microtime(true);
echo (microtime(true) - $time_start);
Will output 0.0034611225128174
$time_start=microtime(true);
for ($i=0; $i < 100000; $i++)
{
$stringArr = array('rando' => 1);
$stringArr['rando']++;
}
$time_end=microtime(true);
echo (microtime(true) - $time_start);
Will output 0.017335176467896
Upvotes: 1
Reputation: 2436
$stringVar = 'iAmSomeRandomString';
Have Total memory usage 179336.
$stringArr = array('rando' => 'iAmSomeRandomString');
Have total memory usage 179616.
According to Memory when we have just single element in array then it is better to use basic variable.
memory_get_usage() a PHP Function can be used for memory allocated to PHP Script.
Upvotes: 1