A PHP array should have how much analysis _php tips

Source: Internet
Author: User

Although a large number of array operations in PHP may be problematic to some extent in the design process, a rough estimate of the memory used by the array is necessary.
First feel the memory of the integer array of 1000 elements:

Copy Code code as follows:

Echo Memory_get_usage (). "\ n";
$a = Array ();
For ($i =0 $i <1000; $i + +) {
$a [$i] = $i + $i;
}
Echo Memory_get_usage (). "\ n";
For ($i =1000 $i <2000; $i + +) {
$a [$i] = $i + $i;
}
Echo Memory_get_usage (). "\ n";

The output is:
58176
162956
267088
Approximately 1000 elements of an array of integers can be known to occupy 100k of memory, with an average of 100 bytes per element. and pure C in the whole only need 4k. The result of the Memory_get_usage () is not entirely occupied by the array, but also includes some of the structures that the PHP run itself allocates, which may be closer to the real space with the arrays generated by the built-in functions:
Copy Code code as follows:

echo "Init mem:". Memory_get_usage (). "\ n";
$a = Array_fill (0, 10000, 1);
echo "10k elements:". Memory_get_usage (). ", System:". Memory_get_usage (True). "\ n";
$b = Array_fill (0, 10000, 1);
echo "10k elements:". Memory_get_usage (). ", System:". Memory_get_usage (True). "\ n";

Get:
Init mem:58468
10k elements:724696, system:786432
10k elements:1390464, system:1572864
From this result it seems that an array element occupies about 60 bytes. Then look at the c structure of the array, the array variables in PHP, first need a ZVAL structure:
Copy Code code as follows:

struct _zval_struct {
Zvalue_value value;
Zend_uint refcount__gc;
Zend_uchar type;
Zend_uchar is_ref__gc;
};

Zvalue_value is a union:
Copy Code code as follows:

typedef Union _ZVALUE_VALUE {
Long lval;
Double Dval;
struct {
Char *val;
int Len;
} str;
HashTable *ht;
Zend_object_value obj;
} Zvalue_value;

Usually the Zval structure requires 8+6=14 bytes, each variable in PHP has a corresponding zval, but the array, string, and object also require an additional storage structure, while the array is a HashTable:
Copy Code code as follows:

typedef struct _HASHTABLE {
UINT Ntablesize;
UINT Ntablemask;
UINT Nnumofelements;
ULONG Nnextfreeelement;
Bucket *pinternalpointer;
Bucket *plisthead;
Bucket *plisttail;
Bucket **arbuckets;
dtor_func_t Pdestructor;
Zend_bool persistent;
unsigned char napplycount;
Zend_bool bapplyprotection;
} HashTable;

The HashTable structure requires 40 bytes, and each array element is stored in the Bucket structure:
Copy Code code as follows:

typedef struct BUCKET {
ULONG H;
UINT Nkeylength;
void *pdata;
void *pdataptr;
struct bucket *plistnext;
struct bucket *plistlast;
struct bucket *pnext;
struct bucket *plast;
Char arkey[1];
} Bucket;

The Bucket structure requires 36 bytes, with the key longer than four bytes appended to the Bucket, and the element value is likely to be a zval structure, and each array assigns an array of Bucket pointers to arbuckets, although it cannot be said that each additional element requires a pointer , but the situation could be even worse. So an array element takes up 54 bytes, not far from the estimate above.
An empty array will occupy at least Zval + (HashTable) + (arbuckets) = 86 bytes, as a variable should have a position in the symbol table, but also an array element, so an empty array variable requires 118 bytes to describe and store. From a space point of view, the average cost of a small array is large, of course, a script will not flood a large number of small arrays, can be a small space cost to obtain programming shortcuts.
However, if you use the array as a container is another scene, the actual application will often encounter multidimensional arrays, and the majority of elements. For example, the one-dimensional array of 10k elements consumes 540k of memory, whereas the 10k x 10 two-dimensional array theory requires only about 6M, but twice as Memory_get_usage, [10k,5,2] 's three-dimensional array consumes 23M, The small array is not going to come out.

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.