使用PHP int的开销是多少? [英] What is the overhead of using PHP int?
问题描述
我一直听说PHP有开销。例如,C ++ int在32位系统上使用4字节,但PHP int使用更多。这个值是多少?
I keep hearing that PHP has overhead. For example a C++ int uses 4 Bytes on a 32 bit system but a PHP int uses more. What is this value?
推荐答案
我需要更多空间而不是评论来扩展马里奥的发现,所以我会添加一个答案而不是。
I need more space than a comment to expand on mario's findings so I'll add an answer instead.
C union
的大小将是其最大成员的大小(可能需要额外的字节数)对齐约束)。对于 zvalue_value
,这将是 obj
,其大小为三个指针(不包括这些指针所需的内存)指针指向):
The size of a C union
will be the size of its largest member (possibly with extra bytes to satisfy alignment constraints). For zvalue_value
, that would be the obj
which has the size of three pointers (not including the memory required for what those pointers point to):
typedef struct _zend_object {
zend_class_entry *ce;
HashTable *properties;
HashTable *guards; /* protects from __get/__set ... recursion */
} zend_object;
在32位系统上, zend_object
将在64位系统上占用24个字节,需要48个字节。因此,无论您在其中存储什么数据,每个 zvalue_value
将至少需要24或48个字节。还有变量的名称,它消耗更多的内存;编译语言通常在编译器完成后丢弃名称,并将值视为简单的字节序列(因此 double
需要8个字节, char
需要一个字节等...)。
On a 32bit system, a zend_object
will take 24 bytes while on a 64bit system it will take 48 bytes. So, every zvalue_value
will take at least 24 or 48 bytes regardless of what data you store in it. There's also the name of the variable which consumes more memory; compiled languages generally discard the names once the compiler is done and treat values as simple sequences of bytes (so a double
takes eight bytes, a char
takes one byte, etc...).
关于你最近关于PHP布尔值的问题,一个简单的布尔值将消耗24或48个字节对于值,再加上几个字节的名称,再加上四个或八个 zend_unit
,再加上四个(或八个)的两个 zend_uchar
s在此:
With regards to your recent questions about PHP booleans, a simple boolean value will consume 24 or 48 bytes for the value, plus a few more bytes for the name, plus four or eight for the zend_unit
, plus four (or eight) for the two zend_uchar
s in this:
struct _zval_struct {
/* Variable information */
zvalue_value value; /* value */
zend_uint refcount__gc;
zend_uchar type; /* active type */
zend_uchar is_ref__gc;
};
zend_uchar
会员将嚼四(或者八个字节由于对齐约束,几乎每个CPU都希望访问自然地址边界上的内存,这意味着 struct
的单个字节大小的成员将占用四个字节或八个字节的内存(取决于CPU的自然字大小和对齐约束)。因此,布尔值将占用36到72个字节的内存。
The zend_uchar
members will chew up four (or eight) bytes due to alignment constraints, almost every CPU wants to access memory on natural address boundaries and that means that a single byte sized member of a struct
will take up four bytes or eight bytes of memory (depending on the CPUs natural word size and alignment constraints). So, a boolean will take somewhere between 36 and 72 bytes of memory.
这篇关于使用PHP int的开销是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!