Let’s just say the conclusion: empty is the most efficient.
count needs to count the length of the array first and then make a judgment.
Some replies pointed out that this is incorrect
I always thought that the time complexity of count was O(n). After I was slapped in the face this time, I read the original code and found out that it is actually O(1).
count source code link
count function operates on arrays:
case IS_ARRAY:
RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC) )
Let’s go through the php_count_recursive operation, and then look at the php_count_recursive function’s operation on the array: cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
zend_hash_num_elements code:
ZEND_API int zend_hash_num_elements(HashTable *ht)
{
IS_CONSISTENT(ht);
return ht->nNumOfElements;
}
isset can only detect whether a variable is defined or has a NULL value. If the variable is defined first, !$array is equivalent to empty. Efficiency is also equal.
typedef struct _HashTable
{
int size;
int elem_num;
Bucket** buckets;
} HashTable;
In implementation, whether empty,还是count,都是取的zVal - value指向的HashTable结构中的elem_num
In other answers, saycount需要计算长度,拜托,PHP会那么傻?还傻乎乎的跑一遍链表,count会直接返回数组的elem_num
Conclusion
So there is no difference between the two judgment methods,empty就是执行了elem_num <= 0. Since both execute the same underlying code, it does not mean that the execution speed is the same.
Speed difference
According to the test results in http://stackoverflow.com/ques... (the test results are below), you will find count的确要比empty慢一点,也许大家会疑惑,既然都是判断的elem_num, so why is it slow?
Why is it slow
According to PHP manual:
There is a line to explain empty:
Because it is a language constructor rather than a function, it cannot be called by variable functions.
So the result is obvious: count是函数,empty却是一个语言构造器 既然是语言构造器,那执行效率是肯定比函数高的,比如echo也是语言构造器,
For example echo 'str1','str2';的效率就比 echo 'str1'.'str2';高,更不用说print
But, after using OpCache or JIT, there is no difference between the two (see below), because both have been optimized.
Native PHP
Empty empty() 0.118691
Empty count() 0.218974
Full empty() 0.133747
Full count() 0.216424
IF empty empty() 0.166474
IF empty count() 0.235922
IF full empty() 0.120642
IF full count() 0.248273
OR empty empty() 0.123875
OR empty count() 0.258665
OR full empty() 0.157839
OR full count() 0.224869
IF/ELSE empty empty() 0.167004
IF/ELSE empty count() 0.263351
IF/ELSE full empty() 0.145794
IF/ELSE full count() 0.248425
( ? : ) empty empty() 0.169487
( ? : ) empty count() 0.265701
( ? : ) full empty() 0.149847
( ? : ) full count() 0.252891
Used JIT (or HipHop VM)
Empty empty() 0.210652
Empty count() 0.212123
Full empty() 0.206016
Full count() 0.204722
IF empty empty() 0.227852
IF empty count() 0.219821
IF full empty() 0.220823
IF full count() 0.221397
OR empty empty() 0.218813
OR empty count() 0.220105
OR full empty() 0.229118
OR full count() 0.221787
IF/ELSE empty empty() 0.221499
IF/ELSE empty count() 0.221274
IF/ELSE full empty() 0.221879
IF/ELSE full count() 0.228737
( ? : ) empty empty() 0.224143
( ? : ) empty count() 0.222459
( ? : ) full empty() 0.221606
( ? : ) full count() 0.231288
isset cannot determine whether the array is empty
The other two: http://stackoverflow.com/ques...
The conclusion is that empty is more efficient than count
Let’s just say the conclusion: empty is the most efficient.
count needs to count the length of the array first and then make a judgment.count source code link
count function operates on arrays:
Let’s go through the php_count_recursive operation, and then look at the php_count_recursive function’s operation on the array:
cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
zend_hash_num_elements code:
isset can only detect whether a variable is defined or has a NULL value.
If the variable is defined first, !$array is equivalent to empty. Efficiency is also equal.
isset
不能判断我就不回答了,这是PHP基础知识,看手册去下面主要讲
empty
和count
The differenceBased on PHP source code (PHP5.4)
In implementation, whether
empty
,还是count
,都是取的zVal - value
指向的HashTable
结构中的elem_num
Conclusion
So there is no difference between the two judgment methods,
empty
就是执行了elem_num <= 0
. Since both execute the same underlying code, it does not mean that the execution speed is the same.Speed difference
According to the test results in http://stackoverflow.com/ques... (the test results are below), you will find
count
的确要比empty
慢一点,也许大家会疑惑,既然都是判断的elem_num
, so why is it slow?Why is it slow
So the result is obvious:
count
是函数,empty
却是一个语言构造器
既然是
语言构造器
,那执行效率是肯定比函数高的,比如echo
也是语言构造器
,For example
echo 'str1','str2';
的效率就比echo 'str1'.'str2';
高,更不用说print
But, after using OpCache or JIT, there is no difference between the two (see below), because both have been optimized.
Native PHP
Used JIT (or HipHop VM)
First of all, if it is not an associative array, isset is more efficient and empty is usually used