Php array and spl fixed array

Source: Internet
Author: User
Tags spl rehash
Php arrays and spl fixed arrays php fixed arrays belong to a data structure of the php Standard Library (spl. Compared with a php ordinary array, a fixed array can only define its subscript using an integer, and as shown in the name, it is a fixed length, which has the advantage of less memory than a common array, and is faster, the specific cause is analyzed below. first, a simple test will be conducted to put 10 million a records into the array. Define (& quot; MAX & quot;, 100000); si php array and spl fixed array
The php fixed array is a data structure of the php Standard Library (spl. Compared with a php ordinary array, a fixed array can only define its subscript using an integer, and as shown in the name, it is a fixed length, which has the advantage of less memory than a common array, and is faster, the specific cause is analyzed below. first, a simple test will be conducted to put 10 million a records into the array.
define("MAX", 100000);//simple arrayfunction simple_arr(){        $i = MAX;$arr = array();while ($i--)$arr[$i]= 'a';}// fix arrayfunction fix_arr(){$i = MAX;$arr = new SplFixedArray(MAX);while ($i--)$arr[$i]= 'a';}//fix array with setfunction fix_set_arr(){$i = MAX;$arr = new SplFixedArray(MAX);while ($i--)$arr->offsetSet($i, "a");}

Time consumption

General array: 0.084696054458618

Fixed array: 0.048405885696411

Fixed array call offsetSet method copy: 0.27650499343872

Memory consumption

General array: 9324672

Fixed array: 4800464

Fixed array call offsetSet method copy: 4800344


Space consumption comparison

From the perspective of space and time efficiency, the consumption of fixed arrays is much less than that of ordinary arrays. Fixed arrays are much slower to assign values through the extended built-in function offsetSet than to assign values through the subscript. because the extended built-in method is used to assign values to arrays, php needs to query multiple nested function tables.

In terms of space, for general arrays, php is internally stored through hashtable. each slot in hashtable corresponds to a value in the array, in php source code Zend/zend_hash.h, struct definitions and functions related to hash are defined.

typedef struct bucket {ulong h;/* Used for numeric indexing */uint nKeyLength;void *pData;void *pDataPtr;struct bucket *pListNext;struct bucket *pListLast;struct bucket *pNext;struct bucket *pLast;const char *arKey;} Bucket;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_DEBUGint inconsistent;#endif} HashTable

As shown in the code above, the space of a php array with 10 elements is sizeof (HashTable) + 10 * size (Bucket) + space occupied by the element itself, which is an arithmetic operation at the code level, in fact, it will be a little complicated inside php. the nTableSize of HashTable is always 2 ^ n, so even if it is 10 elements, php can use 2 ^ 4 through a simple algorithm internally, that is, 16 slots, so the actual space occupied is sizeof (HashTable) + 16 * sizeof (Bucket) + the space occupied by the element itself. (Only when the subscript is an integer is considered for space calculation)

The corresponding fixed array directly initializes the array by the size of the user-transferred person, as shown in the code below: The array of the same 10 elements requires only 10 * of space occupied by the elements themselves.

static void spl_fixedarray_init(spl_fixedarray *array, long size TSRMLS_DC) /* {{{ */{if (size > 0) {array->size = 0; /* reset size in case ecalloc() fails */array->elements = ecalloc(size, sizeof(zval *));array->size = size;} else {array->elements = NULL;array->size = 0;}}

Time comparison

For a fixed array, the memory application is in place in one step. when the memory is insufficient, an error is reported. when the memory is not used up, it is wasted.

For a common array, because the array space is dynamically allocated, because we do not know how many elements are required in advance, php will initialize an empty array with 8 slots by default, but when the slots are not enough, the space of * 2 will be allocated. when the number of element elements is greater than the nTableSize in hashTbale, the resize and rehash hashTable will be performed. in the resize and rehash processes, the time consumption is considerable.

static int zend_hash_do_resize(HashTable *ht){Bucket **t;#ifdef ZEND_SIGNALSTSRMLS_FETCH();#endifIS_CONSISTENT(ht);if ((ht->nTableSize << 1) > 0) {/* Let's double the table size */t = (Bucket **) perealloc_recoverable(ht->arBuckets, (ht->nTableSize << 1) * sizeof(Bucket *), ht->persistent);if (t) {HANDLE_BLOCK_INTERRUPTIONS();ht->arBuckets = t;ht->nTableSize = (ht->nTableSize << 1);ht->nTableMask = ht->nTableSize - 1;zend_hash_rehash(ht);HANDLE_UNBLOCK_INTERRUPTIONS();return SUCCESS;}return FAILURE;}return SUCCESS;}ZEND_API int zend_hash_rehash(HashTable *ht){Bucket *p;uint nIndex;IS_CONSISTENT(ht);if (UNEXPECTED(ht->nNumOfElements == 0)) {return SUCCESS;}memset(ht->arBuckets, 0, ht->nTableSize * sizeof(Bucket *));p = ht->pListHead;while (p != NULL) {nIndex = p->h & ht->nTableMask;CONNECT_TO_BUCKET_DLLIST(p, ht->arBuckets[nIndex]);ht->arBuckets[nIndex] = p;p = p->pListNext;}return SUCCESS;}


Through comparison, we found that the memory and time consumption of php common arrays are mostly used in symbol tables compared with fixed arrays. An important internal implementation idea of php is to exchange space for time and use hashTable to quickly locate data elements. it even designs hashTable as a two-way linked list, in addition, php array space is dynamically allocated, while the internal space is implemented by c. the c language only has fixed allocation of array space. in order to make users feel that php array space is dynamically allocated, it can only be implemented through resize and rehash. Therefore, when assigning values to the same number of elements than fixed arrays, the time will be slower. In short, regular arrays and fixed arrays have their own advantages and disadvantages. it cannot be said that after the time and space consumption of fixed arrays are low, specific business scenarios need to be taken into account.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.