How does PHP memory actually work

前端 未结 4 1951
你的背包
你的背包 2020-12-14 17:27

I\'ve always heard and searched for new php \'good writing practice\', for example: It\'s better (for performance) to check if array key exists than search in array, but als

4条回答
  •  醉酒成梦
    2020-12-14 18:10

    Although both arrays are accessed in a different way (i.e. via string or integer value), the memory pattern is mostly similar.

    This is because the string allocation either happens as part of the zval creation or when a new array key needs to be allocated; the small difference being that numeric indices don't require a whole zval structure, because they're stored as an (unsigned) long.

    The observed differences in memory allocation are so minimal that they can be largely attributed to either the inaccuracy of memory_get_usage() or allocations due to additional bucket creation.

    Conclusion

    How you want to use your array must be the guiding principle in choosing how it should be indexed; memory should only become an exception to this rule when you run out of it.

提交回复
热议问题