Recently during a casual conversation someone asked me what would be the fastest way to check for array presence in PHP. Normally you don’t tend to think of the empty array as anything special, but they do occur a lot - a query from the database can return 0 rows, the object that has been in cache has expired, or the Web page has 0 inbound links pointing to it. Pretty usual way to go about it i to use count or its twin brother sizeof to find out the length of the array and behave accordingly depending on whether the if statement returns true or not.
It was pointed out to me, however, that both count and sizeof always need to conduct the pointer arithmetics, even if the array in question is of zero size. Checking for the existence of the first element of the array, however, would involve no pointer arithmetics, since referencing $array[0] is the same as referencing $array pointer. But how much faster is it? I set out to test it with a pretty simple script that would run both comparisons a million times. Since accessing the first element of an empty array would generate a PHP notice (note that it’s not an error, the pointer is still valid), I had to revert to error_reporting(0) at the beginning of the file to exclude the time spent on printing out PHP notices.
It’s also useful to know that whenever the if statement contains a clause that doesn’t have executable code, it will be optimized by the interpreter, and therefore something should be done inside the if() clause once we’re there. So how about running a counter from 1 to a million, checking for the same array over and over, and printing the counter value on each run?
error_reporting(0);
$array1 = array();
$time1 = time();
for ($x=0; $x<1000000; $x++)
{
if (count($array1)==0)
{echo $x;
}
}
$time2 = time();
for ($x=0; $x<1000000;$x++)
{
if(!$array1[0])
{echo $x;
}
}
$time3 = time();
echo ($time2-$time1).”\n”;
echo ($time3-$time2).”\n”;
The results?
99937
36
More than 2,500x difference.
You assume that if there is no 0th element then array is empty. Is it correct assumption?
I thought that PHP’s arrays are associative arrays and there can be $array['abc'] but not $array[0].
In any case, your solution might be much better if you know that array is not associative.
Also, what will happen in your code if $array1[0] equals 0 - will it return true or false?
Ok, I stand corrected, if (isset($array[0])) is the fastest option and it accounts for $array[0] being equal to zero.
You should be careful with this syntax, as this will cause problems when $array is a string because $a[0] will be set to the first character of the string.
Personally, I find is_array() to be far and away the most clearly named of all the PHP functions you can use to test is a variable is an array, so I always use that regardless of any minimal speed benefit another function may have.
Sorry for lifting this post up… But how abaout empty() ???
I hate to burst your bubble but is_array is faster. Looking at your code you could have used microtime (with the parameter set to true), better yet you should have used PEAR’s Benchmark. I did and comparing isset($array[0]) to is_array came up with:
Below results where when the function were checking the integer 123.
Mean execution time for is_array: 0.000121051421165
Mean execution time for isset: 0.000126702299118
Below results where when the function were checking the array (1,2,3)
Mean execution time for is_array: 0.000145638279915
Mean execution time for isset: 0.000153319172859
Also note that the count() function does not actually count the number of elements. If you look at the struct for a PHP variable (the same for all data types) it’s simple an attribute that count reads which gets changed whenever the arrays length ever changes.
Considering is_array is easier to read, catches more “edge” cases (if they can be called that) and it slightly quick I’d say that’s what should be used.
Dixon: empty could simply return the wrong information and it’s actually the same speed as is_array anyway,