亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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就是執(zhí)行了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卻是一個語言構造器
既然是語言構造器,那執(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.

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