使用 PHP int 的开销是多少? [英] What is the overhead of using PHP int?

查看:28
本文介绍了使用 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?

推荐答案

我需要比评论更多的空间来扩展 mario 的发现,所以我将添加一个答案.

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 将占用 24 个字节,而在 64 位系统上将占用 48 个字节.因此,每个 zvalue_value 将至少占用 24 或 48 个字节,无论您在其中存储什么数据.还有消耗更多内存的变量的名称;一旦编译器完成,编译语言通常会丢弃名称并将值视为简单的字节序列(因此 double 占用八个字节, 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 个字节作为值,再加上几个字节作为名称,再加上 4 或 8 个字节作为 zend_unit, 加上四个(或八个)来表示这两个 zend_uchar:

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_uchars 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 都希望访问自然地址边界上的内存,这意味着 zend_uchar 成员将占用四个(或八个)字节code>struct 将占用 4 个字节或 8 个字节的内存(取决于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆