Should I use an associative array or an object?

后端 未结 3 552
别跟我提以往
别跟我提以往 2021-02-01 16:23

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 we

相关标签:
3条回答
  • 2021-02-01 17:06

    Performance Benchmark (access time)

    Here is my benchmark. I was mostly interested in access time. I populated an array with 10,000 variables, cast it as an object, then for both the object and array I simply accessed one of the variables 10,000 times. Part of the code:

    $arr = array();
    for( $i=0; $i<10000; $i++ ) {
        $arr['test'.$i] = 'Hello. My name is Inigo Montoya. You killed my father. Prepare to die.';
    }
    $obj = (object)$arr;
    
    $tests = array(0,1000,2000,3000,4000,5000,6000,7000,8000,9999);
    
    foreach( $tests as $test ) {
        $test_name = 'test'.$test;
    
        $start = microtime(true);
        for( $i=0; $i<10000; $i++ ) {
            $var = $obj->$test_name;
        }
        $end = microtime(true);
        $elapsed = $end - $start;
    
        $start = microtime(true);
        for( $i=0; $i<10000; $i++ ) {
            $var = $arr[$test_name];
        }
        $end = microtime(true);
        $elapsed = $end - $start;
    }
    

    Results

    I ran the test multiple times; here is one of the typical result sets; times are in milliseconds.

                Object    Array
    ------------------------------
    test0       4.4880    4.1411
    test1000    4.5588    4.2078
    test2000    4.5812    4.2109
    test3000    4.5240    4.2000
    test4000    4.5800    4.2648
    test5000    4.5929    4.2000
    test6000    4.5311    4.2260
    test7000    4.6101    4.2901
    test8000    4.5331    4.1370
    test9999    4.5100    4.1430
    

    The array was an average of 8.3% faster than the object (7.7% in the set above). The index of the variable we are trying to access has no effect on the access time.

    Seeing the comments above I'm embarrassed to say I'm on PHP 5.3.4.

    0 讨论(0)
  • 2021-02-01 17:18

    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.

    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.


    Performance Benchmark (parsing)

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

    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:

    1st run

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

    2nd run

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

    3rd run

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

    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.

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

    1st run

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

    2nd run

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

    3rd run

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

    PHP version

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

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

    Technologies

    0 讨论(0)
  • 2021-02-01 17:25

    Not entirely sure off the top of my head, but you could write something simple and then use FireFox's firebug to see what the round trip of the post was in ms.

    0 讨论(0)
提交回复
热议问题