使用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我需要比评论更多的空间来扩展马里奥的发现,所以我将添加一个答案。
C 联合体的大小将是其最大成员的大小(可能有额外的字节来满足对齐约束)。对于
zvalue_value
,这将是obj
,其大小为三个指针(不包括这些指针指向的内容所需的内存):在 32 位系统上,< code>zend_object 将占用 24 个字节,而在 64 位系统上它将占用 48 个字节。因此,每个
zvalue_value
将至少占用 24 或 48 个字节,无论您在其中存储什么数据。还有消耗更多内存的变量名称;编译语言通常会在编译器完成后丢弃名称,并将值视为简单的字节序列(因此,double
需要八个字节,char
需要一个字节,等等。 .)。关于您最近有关 PHP 布尔值的问题,一个简单的布尔值将消耗 24 或 48 个字节的值,再加上几个字节的名称,加上 4 或 8 个
zend_unit
字节,再加上 4 个字节。 (或八个)对于两个zend_uchar
来说:由于对齐限制,
zend_uchar
成员将占用四个(或八个)字节,几乎每个 CPU 都想要访问内存在自然地址边界上,这意味着 struct 的单个字节大小的成员将占用四个字节或八个字节的内存(取决于 CPU 的自然字大小和对齐约束)。因此,布尔值将占用 36 到 72 字节的内存。I need more space than a comment to expand on mario's findings so I'll add an answer instead.
The size of a C
union
will be the size of its largest member (possibly with extra bytes to satisfy alignment constraints). Forzvalue_value
, that would be theobj
which has the size of three pointers (not including the memory required for what those pointers point to):On a 32bit system, a
zend_object
will take 24 bytes while on a 64bit system it will take 48 bytes. So, everyzvalue_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 adouble
takes eight bytes, achar
takes one byte, etc...).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 twozend_uchar
s in this: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 astruct
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 不仅仅存储 C
int
。它需要保留每个值的类型信息和其他信息。每个变量还需要变量范围哈希表之一中的一个条目。不确定这是否是正确的代码片段,但基本上在 PHP 源代码中查找
zval
:大多数类似整数的类型至少使用
long
。 (我认为其中包括您之前问题中的布尔值。)http://porteightyeight.com/2008/03/18/the-truth-about-php-variables/
PHP does not just store an C
int
. It needs to keep type information and whatnot for each value. Each variable also needs an entry in one of the variable scope hash tables.Not sure if this is the right snippet, but basically look for
zval
in the PHP source:Most integer-like types use at least a
long
. (Which I assume would include the booleans from your previous questions.)http://porteightyeight.com/2008/03/18/the-truth-about-php-variables/