PHP determines whether an array is empty, which one is more efficient?
仅有的幸福
仅有的幸福 2017-05-16 12:58:57
0
4
837

isset count empty

仅有的幸福
仅有的幸福

reply all(4)
刘奇

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.

Peter_Zhu

isset不能判断我就不回答了,这是PHP基础知识,看手册去
下面主要讲emptycountThe difference

Based on PHP source code (PHP5.4)

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
阿神

First of all, if it is not an associative array, isset is more efficient and empty is usually used

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template