我应该使用关联数组还是对象? [英] Should I use an associative array or an object?

查看:80
本文介绍了我应该使用关联数组还是对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,json_decode使您可以选择返回关联数组或对象.在许多其他情况下,我们也有两个选择.显然,在某些情况下,根据要处理的数据类型(与一个项目相关的一组数据对一个项目的列表而言),使用一种或另一种更为合适.

As we all know, json_decode gives you the option of returning an associative array or an object. There are many other situations where we have the two options as well. Obviously in some cases using one or the other is more "appropriate" based on the type of data you're dealing with (a group of data pertaining to one item vs. a list of items).

我想知道的是,在内存,速度等方面,使用一种与另一种相比效率有何不同?我对非常大的对象的访问时间特别感兴趣-与大型数组相比,为什么?

What I'm wondering is, is there any difference in efficiency in using one vs. the other, in terms of memory, speed, etc? I'm particularly interested in access time for a very large object - how does that compare to a very large array, and why?

很抱歉,如果以前没有讨论过,搜索什么都没有.我正在写一个基准,它可能会告诉我哪一个更好,但并不能帮助我理解为什么.

Sorry if this has been discussed before, search turned up nothing. I'm writing a benchmark, which may tell me which one is better, but won't help me understand why.

推荐答案

许多程序员更喜欢使用true作为

Many programmers prefer using true as the second argument to json_decode since the returned assoc array will be very similar to how you handle objects in javascript.

返回一个适当的对象将需要了解如何使用和不使用这种对象,并且由于大多数程序员对关联数组更为熟悉,因此最好使用关联数组,尤其是如果代码将由一组开发人员维护时.代码应该易于理解.

Returning a proper object will require reading about how such is used and what not, and since most programmers are well familiar with associative arrays that's more preferrable, especially if the code will be maintained by a team of developers. Code should be easily understandable.

关于性能的问题,我认为您不必担心,因为在大多数情况下,瓶颈都在其他地方.除非您要解析一个很大的字符串,否则我的意思是说非常大,则您无需进行任何基准测试.我相信,返回一个assoc数组与一个合适的对象之间的区别很小.

Regarding questions about performance I don't think you'll need to worry about that since the bottle neck in most (all) cases will be elsewhere. Unless you are parsing a massive string, and by that I mean really huge, you shouldn't need to make any benchmarks. I believe the difference between returning an assoc array vs a proper object will be minor.

我在此处找到了一个较大的json字符串,进行了一些调整以使其更大,最终大小为84 578个字节.

I found a rather large json string here and made some adjustments to make it even bigger, final size is 84 578 bytes.

然后,我使用这两种选择(关联数组与对象)分别对字符串进行了1000次解析,然后对测试进行了3次测试.结果如下:

I then parsed the string using both alternatives (associative array vs object) 1 000 times each, and I ran the test three times. The results are given below:

第一次运行

  JSON object exec: 4.06122 s
  JSON assoc  exec: 3.28679 s
-------------------------------------
assoc is faster by 19.07%

第二次运行

  JSON object exec: 4.09614 s
  JSON assoc  exec: 3.29216 s
-------------------------------------
assoc is faster by 19.63%

第三次运行

  JSON object exec: 4.08762 s
  JSON assoc  exec: 3.29960 s
-------------------------------------
assoc is faster by 19.28%


性能基准(读/写)

此基准测试是为了显示stdObjectArray()中的哪个更快,我使用的是经过解析的修改后的json文件(更大的文件).


Performance Benchmark (read/write)

This benchmark is to show which one of stdObject and Array() is faster, I'm using a parsed modified json file (bigger one) than in the previous benchmark.

每个读/写测试运行了10万次(即下面给出的代码执行了多次).

Each read/write test was run 100 000 times (ie. the code given below was executed that many times).

json_decode($ json_data)

for ($i =0; $i < 24; ++$i){
  $a = $object[$i]->user->profile_sidebar_border_color . "stackoverflow";
  $object[$i]->nested->entities->user_mentions[0]->indices[$i&1] += 1;
}

json_decode($ json_data,true)

for ($i =0; $i < 24; ++$i){
  $a = $assoc[$i]['user']['profile_sidebar_border_color'] . "stackoverflow";
  $assoc[$i]['nested']['entities']['user_mentions'][0]['indices'][$i&1] += 1;
}

第一次运行

  JSON object read/write: 3.05421 s
  JSON assoc  read/write: 2.51932 s
-------------------------------------
assoc is faster by 17.51%

第二次运行

  JSON object read/write: 3.06307 s
  JSON assoc  read/write: 2.52701 s
-------------------------------------
assoc is faster by 17.50%

第三次运行

  JSON object read/write: 3.06109 s
  JSON assoc  read/write: 2.52248 s
-------------------------------------
assoc is faster by 17.60%


PHP版本

PHP 5.3.6(cli)(内置:2011年8月13日19:04:57)版权所有(c)1997-2011

PHP 5.3.6 (cli) (built: Aug 13 2011 19:04:57) Copyright (c) 1997-2011

PHP Group Zend Engine v2.3.0,版权所有(c)1998-2011 Zend

The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend

技术

这篇关于我应该使用关联数组还是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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