By accident, Baidu knows that a student asked about the efficiency of count and strlen. well, I didn't fully understand this problem at the beginning and thought it was not two different things that could not be compared, after reading the reply from the landlord, I checked the source code. At present, zend is used to store all the variables in php in a structured manner, and the saving of strings is different from that of arrays, the array is saved as a hash table (you know the concept that the address saved by hash effectively reduces the conflict-hash list), and the structure in php is shown as follows:
Copy codeThe code is as follows:
// File 1: zend/zend. h
/*
* Zval
*/
Typedef struct _ zval_struct zval;
...
Typedef union _ zvalue_value {
Long lval;/* long value */
Double dval;/* double value */
Struct {
Char * val;
Int len;
} Str;
HashTable * ht;/* hash table value */
Zend_object_value obj;
} Zvalue_value;
Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount _ gc;
Zend_uchar type;/* active type */
Zend_uchar is_ref _ gc;
};
// The structure of the hash table is as follows:
// File 2: zend/zend_hash.h
Typedef struct _ hashtable {
Uint nTableSize;
Uint nTableMask;
Uint nNumOfElements;
Ulong nNextFreeElement;
Bucket * pInternalPointer;/* Used for element traversal */
Bucket * pListHead;
Bucket * pListTail;
Bucket ** arBuckets;
Dtor_func_t pDestructor;
Zend_bool persistent;
Unsigned char nApplyCount;
Zend_bool bApplyProtection;
# If ZEND_DEBUG
Int inconsistent;
# Endif
}
HashTable;
When strlen is used to obtain the length of a common variable (string), The len attribute in the zvalue_value.str structure is actually obtained, and the efficiency is O (1). It is particularly noted that: strlen has no core implementation in php, but is obtained by using the macro definition in zend:
Copy codeThe code is as follows:
// File 3: zend/zend_operators.php
# Define Z_STRLEN (zval). value. str. len
...
# Define Z_STRLEN_P (zval_p) Z_STRLEN (* zval_p)
...
# Define Z_STRLEN_PP (zval_pp) Z_STRLEN (** zval_pp)
In fact, there are two results for the count operation on the array. in the count api, the second parameter mode "limit (N) [N: length] is also mentioned. by default, nNumOfElements in hashtable will be directly output without re-statistics. The efficiency is also O (1) times: the count code is as follows:
Copy codeThe code is as follows:
// File 4: ext/standard/array. c
PHP_FUNCTION (count)
{
Zval * array;
Long mode = COUNT_NORMAL;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service