PHP:数组深度如何影响性能?

时间:2022-07-05 21:21:31

Now I know there is some related questions on this topic but this is somewhat unique.

现在我知道有关于这个主题的一些相关问题,但这有点独特。

I have two array structures :

我有两个数组结构:

array(
    [0] => array(
        'stat1' => 50,
        'stat2' => 12,
        'stat3' => 0,
        'country_name' => 'United States'
        ),
    [1] => array(
        'stat1' => 40,
        'stat2' => 38,
        'stat3' => 15,
        'country_name' => 'Ireland'
        ),
    [2] => array(
        'stat1' => 108,
        'stat2' => 0,
        'stat3' => 122,
        'country_name' => 'Autralia'
        )
    )

and the second

第二个

array(
    'stat1' => array(
        'countries' => array(
            'United States' => 50,
            'Ireland' => 40,
            'Autralia' => 108,
            )
        )
    ),
'stat2' => array(
        'countries' => array(
            'United States' => 12,
            'Ireland' => 38,
            )
        )
    ),
etc...

The second array can go even to level 4 or 5 if you add the cities of those respective countries. Further to note is that the second array structure will have no 0 data fields (note that in the second one australia is not there because it is 0) but the first structure will have a whole whack of zeros. Also note that the second structure will have duplicates i.e. 'United States'

如果添加这些国家/地区的城市,则第二个阵列甚至可以达到4级或5级。需要注意的是,第二个数组结构将没有0个数据字段(请注意,在第二个数组中,澳大利亚不存在,因为它是0),但第一个结构将有一个零的整数。另请注意,第二个结构将有重复,即'美国'

My question is this: How does these array structures compare when they are json_encode() and used in a POST ajax request? Will the shallow depth array, with it's whack of zeros be faster or will the better structured array be better in terms of size?

我的问题是:当它们是json_encode()并在POST ajax请求中使用时,这些数组结构如何比较?浅层深度阵列是否会更快或者更好的结构化阵列在尺寸方面会更好?

1 个解决方案

#1


1  

I have done some testing and for a finite dataset the difference in the output data - (I outputted the data into a text file) between the two is insignificant really.

我做了一些测试,对于有限的数据集,输出数据的差异 - (我将数据输出到文本文件中)两者之间的差别是微不足道的。

Array structure 1 - All city and country data outputs to 68kb

阵列结构1 - 所有城市和国家数据输出为68kb

Array structure 2 - All city and country data outputs to 71kb

阵列结构2 - 所有城市和国家数据输出为71kb

So there is a slight difference but it seems that the difference is insignificantly small when taking into account that the data is in JSON format and used in an AJAX request to the google visualization geomap API.

因此存在细微差别,但考虑到数据是JSON格式并且在用于Google可视化geomap API的AJAX请求中使用时,差异似乎微不足道。

I haven't tested the micro times in loading difference but for a user to look at a loading .gif image for 0.0024microseconds (i'm shooting a random time for the sake of argument) does not make a big dent in usability either way. Thanx all for you comments

我没有测试加载差异的微时间,但是对于用户来说,看看加载的.gif图像为0.0024微秒(我为了参数而拍摄一个随机时间)不会对可用性产生很大的影响。 Thanx全部为你评论

#1


1  

I have done some testing and for a finite dataset the difference in the output data - (I outputted the data into a text file) between the two is insignificant really.

我做了一些测试,对于有限的数据集,输出数据的差异 - (我将数据输出到文本文件中)两者之间的差别是微不足道的。

Array structure 1 - All city and country data outputs to 68kb

阵列结构1 - 所有城市和国家数据输出为68kb

Array structure 2 - All city and country data outputs to 71kb

阵列结构2 - 所有城市和国家数据输出为71kb

So there is a slight difference but it seems that the difference is insignificantly small when taking into account that the data is in JSON format and used in an AJAX request to the google visualization geomap API.

因此存在细微差别,但考虑到数据是JSON格式并且在用于Google可视化geomap API的AJAX请求中使用时,差异似乎微不足道。

I haven't tested the micro times in loading difference but for a user to look at a loading .gif image for 0.0024microseconds (i'm shooting a random time for the sake of argument) does not make a big dent in usability either way. Thanx all for you comments

我没有测试加载差异的微时间,但是对于用户来说,看看加载的.gif图像为0.0024微秒(我为了参数而拍摄一个随机时间)不会对可用性产生很大的影响。 Thanx全部为你评论