简体   繁体   English

将PHP_INT_MAX与数组一起使用会对性能产生影响吗?

[英]Is there a performance impact when use PHP_INT_MAX with array?

For example I have the following code 例如我有以下代码

$array = [];
$array[0] = 0;
$array[1] = 1;
$array[2] = 'max';

foreach ($array as $value) {
    // code
}

I want to know is there any performance impact when I use PHP_INT_MAX as the index of a array like so. 我想知道,当我使用PHP_INT_MAX作为数组的索引时,会对性能产生任何影响。

$array = [];
$array[0] = 0;
$array[1] = 1;
$array[PHP_INT_MAX] = 'max';

foreach ($array as $value) {
    // code
}

Regardless whether yes or not, anybody can tell me the reason? 不管是与否,有人可以告诉我原因吗? Thanks! 谢谢!

No, the array still only has 3 elements, so only 3 elements exist in memory. 不,数组仍然只有3个元素,因此内存中只有3个元素。

It doesn't really matter what the keys are for those elements, though there's a slight memory overhead for long string keys because PHP maintains the key as well as the value and longer strings use more memory than shorter strings; 这些元素的键实际上并不重要,尽管长字符串键会占用少量内存,因为PHP会维护键以及值,并且长字符串比短字符串会占用更多的内存。 but in this case, your keys are integers which are fixed size, whether the value is -1, 0, 98 or PHP_INT_MAX 但是在这种情况下,您的键是固定大小的整数,无论​​该值是-1、0、98还是PHP_INT_MAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM