使用 PHP int 的开销是多少?

发布于 2024-11-06 06:20:24 字数 72 浏览 1 评论 0原文

我一直听说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-11-13 06:20:24

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

C 联合体的大小将是其最大成员的大小(可能有额外的字节来满足对齐约束)。对于 zvalue_value,这将是 obj,其大小为三个指针(不包括这些指针指向的内容所需的内存):

typedef struct _zend_object {
    zend_class_entry *ce;
    HashTable *properties;
    HashTable *guards; /* protects from __get/__set ... recursion */
} zend_object;

在 32 位系统上,< code>zend_object 将占用 24 个字节,而在 64 位系统上它将占用 48 个字节。因此,每个 zvalue_value 将至少占用 24 或 48 个字节,无论您在其中存储什么数据。还有消耗更多内存的变量名称;编译语言通常会在编译器完成后丢弃名称,并将值视为简单的字节序列(因此,double 需要八个字节,char 需要一个字节,等等。 .)。

关于您最近有关 PHP 布尔值的问题,一个简单的布尔值将消耗 24 或 48 个字节的值,再加上几个字节的名称,加上 4 或 8 个 zend_unit 字节,再加上 4 个字节。 (或八个)对于两个 zend_uchar 来说:

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 字节的内存。

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). 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;

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...).

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;
};

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.

荒人说梦 2024-11-13 06:20:24

PHP 不仅仅存储 C int。它需要保留每个值的类型信息和其他信息。每个变量还需要变量范围哈希表之一中的一个条目。

不确定这是否是正确的代码片段,但基本上在 PHP 源代码中查找 zval

struct _zval_struct {
        /* Variable information */
        zvalue_value value;             /* value */
        zend_uint refcount__gc;
        zend_uchar type;        /* active type */
        zend_uchar is_ref__gc;
};

typedef union _zvalue_value {
        long lval;                                      /* long value */
        double dval;                            /* double value */
        struct {
                char *val;
                int len;
        } str;
        HashTable *ht;                          /* hash table value */
        zend_object_value obj;
} zvalue_value;

大多数类似整数的类型至少使用 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:

struct _zval_struct {
        /* Variable information */
        zvalue_value value;             /* value */
        zend_uint refcount__gc;
        zend_uchar type;        /* active type */
        zend_uchar is_ref__gc;
};

typedef union _zvalue_value {
        long lval;                                      /* long value */
        double dval;                            /* double value */
        struct {
                char *val;
                int len;
        } str;
        HashTable *ht;                          /* hash table value */
        zend_object_value obj;
} zvalue_value;

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文