Reputation: 972
I have a question, what is more faster ...
Upvotes: 21
Views: 9837
Reputation: 39
Many variables are faster than an array mainly because an array is an ordered map (http://php.net/manual/en/language.types.array.php) and therefore by definition for each and every value in the array, a reference key to the value in the array is also assigned. This costs extra memory and can have significant impact when speaking of hundreds of thousands of records in an array. The performance decreases as the number of records increases. This overhead can be avoided by using scalar variables instead of arrays.
Recently I was working on a profiler to measure memory and execution time using a tick callback. At first I used an array to log events and to keep track of all the measurements at each tick interval. After putting these values in separate scalar variables instead of in the array, I discovered that performance increased significantly (between 10 - 20% with 100000 - 1000000 records respectively).
However, when an array only has a few records, the difference with scalar variables is nihil and performance should not really be an issue. In such cases arrays might suit the situation better and be more effective, improve readability, maintenance and so on.
Upvotes: 3
Reputation: 23542
Short: Accessing a variable is faster.
But still you might use arrays because of better code quality. To get better performance use caching. Anyway you should handle performance problems only when they occur!
$n = 1000000;
$startTime = microtime(true);
for ($i = 0; $i <= $n; $i++)
{
$x = $a[1];
}
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
echo "Array: $elapsed seconds";
$startTime = microtime(true);
for ($i = 0; $i <= $n; $i++)
{
$x = $v;
}
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
echo "Variable : $elapsed seconds";
Array: 0.092 seconds
Variable : 0.064 seconds
Also node that using arrays with string as index will be much slower (hashmap). Compare zend_hash_find vs zend_hash_index_find.
How big the array is does not really make a difference if I use $a = array_fill( 0, 1000, 1 );
and $x = $a2[999];
Not ask but also interesting is the memory usage. So I created an array with 10000 elements and 10000 variables.
$a = array();
for ($i = 0; $i <= 10000; $i++)
{
// one array
eval('$a[] = "test";'); // 3454128
// Variables
//eval('$a' . $i. ' = "test";'); // 3552360
}
print_r(memory_get_usage());
Array: 3454128
Variables: 3552360
So arrays do use slightly less memory.
Upvotes: 22
Reputation: 12815
People here say that arrays are faster. But arrays are also variables. if you use an array - you still need to access it like any variable and additionally you need to access an item in array. So, it looks to me that array used like a storage for variables is not the best idea.
Additionally - arrays are used to store some array data. Like category id => category name pairs, for instance.
$catId1 = "Category 1";
$catId2 = "Category 2";
$catId3 = "Category 3";
Code like above would be... strange. You are loosing many features of an array, for instance, can't go through all categories in for
loop. So, for array data - array is what you need.
Once you have different kinds of data (talking about meaning of that data, not its type like integer or string) you should better use variables:
$requested_category = 1;
$requested_category_name = "Some category";
$category_processing_result = "Ok";
instead of array:
$varsArray['requested_category'] = 1;
$varsArray['requested_category_name'] = "Some category";
$varsArray['category_processing_result'] = "Ok";
With variables any IDE will help you to write those names, such code is easier to read and support. And that is more important, as for me.
Even if they are slower somehow, or take more memory - that is not a worst problem in terms of speed/memory usage for sure.
Upvotes: 4
Reputation: 5809
Just a try :)
With 5 variables
$myvar1 = 'hello';
$myvar2 = 'hello';
$myvar3 = 'hello';
$myvar4 = 'hello';
$myvar4 = 'hello';
print_r(memory_get_usage());
Resut : 618600
with 5 array keys
$myvar = array();
$myvar['var1'] = 'hello';
$myvar['var2'] = 'hello';
$myvar['var3'] = 'hello';
$myvar['var4'] = 'hello';
$myvar['var5'] = 'hello';
print_r(memory_get_usage());
Resut : 620256
Upvotes: 26
Reputation: 384
Storing all your data in one array could be faster, depending on how you're handling your data. I would recommend grouping each group of data that has a close relation to eachother in an array.
Hope this helps.
Upvotes: 1