isset count empty
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.
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.
isset
不能判斷我就不回答了,這是PHP基礎知識,看手冊去
下面主要講empty
和count
The difference
typedef struct _HashTable
{
int size;
int elem_num;
Bucket** buckets;
} HashTable;
In implementation, whether empty
,還是count
,都是取的zVal - value
指向的HashTable
結構中的elem_num
In other answers, say
count
需要計算長度,拜托,PHP
會那么傻?還傻乎乎的跑一遍鏈表,count
會直接返回數組的elem_num
So there is no difference between the two judgment methods,empty
就是執(zhí)行了elem_num <= 0
. Since both execute the same underlying code, it does not mean that the execution speed is the same.
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?
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
卻是一個語言構造器
既然是語言構造器
,那執(zhí)行效率是肯定比函數高的,比如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.
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
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
First of all, if it is not an associative array, isset is more efficient and empty is usually used