PHP Objects vs Arrays — Performance comparison while iterating

前端 未结 10 1707
青春惊慌失措
青春惊慌失措 2020-11-27 11:25

I have a huge amount of PHP objects for a neural network for which I have to iterate over and perform some maths on. I was wondering if I would be better off using an associ

相关标签:
10条回答
  • 2020-11-27 12:10

    I use magallanes' code under php 7.0.9 :

    arrays: 0.19802498817444
    
    memory: 324672
    
    Array
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    arrays: 0.18602299690247
    
    memory: 132376
    
    SomeClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    arrays: 0.1950249671936
    
    memory: 348296
    
    stdClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    

    And user php 7.1.3:

    arrays: 0.59932994842529
    memory: 444920
    Array
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 0.72895789146423
    memory: 164512
    
    SomeClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 0.61777496337891
    memory: 484416
    stdClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )                      
    
    0 讨论(0)
  • 2020-11-27 12:11

    magallanes' script @ PHP 7.3.5

    • SomeClass Object is fastest and lightest.
    • Array 1.32x speed. 2.70x memory.
    • stdClass Object 1.65x speed. 2.94x memory.

    Raw output:

    arrays: 0.064794063568115
    memory: 444920
    Array (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    arrays: 0.048975944519043
    memory: 164512
    SomeClass Object (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    arrays: 0.081161022186279
    memory: 484416
    stdClass Object (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    0 讨论(0)
  • 2020-11-27 12:14

    Based in the code of Quazzle, i ran the next code (5.4.16 windows 64bits):

    <?php
    class SomeClass {
        public $aaa;
        public $bbb;
        public $ccc;
        }
    
    function p($i) {
      echo '<pre>';
      print_r($i);
      echo '</pre>';
    }
    
    
    $t0 = microtime(true);
    $arraysOf=array();
    $inicio=memory_get_usage(); 
    for ($i=0; $i<1000; $i++) {
        $z = array();
        for ($j=0; $j<1000; $j++) {
            $z['aaa'] = 'aaa';
            $z['bbb'] = 'bbb';
            $z['ccc'] = $z['aaa'].$z['bbb'];            
        }
        $arraysOf[]=$z;
    }
    $fin=memory_get_usage();    
    echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
    echo '<p>memory: '.($fin-$inicio)."</p>";
    p($z);
    
    $t0 = microtime(true);
    $arraysOf=array();
    $inicio=memory_get_usage(); 
    for ($i=0; $i<1000; $i++) {
        $z = new SomeClass();
        for ($j=0; $j<1000; $j++) {
            $z->aaa = 'aaa';
            $z->bbb = 'bbb';
            $z->ccc = $z->aaa.$z->bbb;          
        }
        $arraysOf[]=$z;
    }
    $fin=memory_get_usage();    
    echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
    echo '<p>memory: '.($fin-$inicio)."</p>";
    p($z);
    
    $t0 = microtime(true);
    $arraysOf=array();
    $inicio=memory_get_usage(); 
    for ($i=0; $i<1000; $i++) {
        $z = new stdClass();
        for ($j=0; $j<1000; $j++) {
            $z->aaa = 'aaa';
            $z->bbb = 'bbb';
            $z->ccc = $z->aaa.$z->bbb;          
        }
        $arraysOf[]=$z;
    }
    $fin=memory_get_usage();    
    echo '<p>arrays: '.(microtime(true) - $t0)."</p>";
    echo '<p>memory: '.($fin-$inicio)."</p>";
    p($z);  
    ?>
    

    And i obtained the next result:

    arrays: 1.8451430797577
    
    memory: 460416
    
    Array
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 1.8294548988342
    
    memory: 275696
    
    SomeClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 2.2577090263367
    
    memory: 483648
    
    stdClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    

    Conclusion for php 5.4

    1. Class is fasts than Arrays (but marginally).
    2. stdClass is evil.
    3. Class uses less memory than Arrays. (about 30-40% less!!)

    ps: as a note, if the class is defined but the members then, the use of this class is slower. It also uses more memory. Apparently the secret is to define the members

    Update

    I updated from php 5.4 to php 5.5 (5.5.12 x86 windows).

    arrays: 1.6465699672699
    
    memory: 460400
    
    Array
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 1.8687851428986
    
    memory: 363704
    
    SplFixedArray Object
    (
        [0] => aaa
        [1] => bbb
        [2] => aaabbb
    )
    
    arrays: 1.8554251194
    
    memory: 275568
    
    SomeClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    arrays: 2.0101680755615
    
    memory: 483656
    
    stdClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    

    Conclusion for php 5.5

    1. For arrays, PHP 5.5 is faster than PHP 5.4, for object it is pretty much the same
    2. Class is slower than Arrays thanks to the optimization of PHP 5.5 and arrays.
    3. stdClass is evil.
    4. Class still uses less memory than Arrays. (about 30-40% less!!).
    5. SplFixedArray is similar to use a Class but it uses more memory.
    0 讨论(0)
  • 2020-11-27 12:14

    I see this is kind of an old post so I thought I'll update it. here are my codes and stats, done on Zend CE 5.3.21 I tried to test the whole thing, storing info and pulling it back.

    V1 : takes 0.83 sec

    for ($i=1; $i<1000000; $i++) {
      $a = get_one();
      $b = $a[0];
      $b = $a[1];
    }
    
    function get_one() {
      return array(1,1);
    }
    

    V2 : takes 3.05 sec

    for ($i=1; $i<1000000; $i++) {
      $a = get_one();
      $b = $a->v;
      $b = $a->k;
    }
    
    function get_one() {
      $ret = new test();
      $ret->v = 1;
      $reb->k = 1;
      return $ret;
    }
    
    class test {
      public $v;
      public $k;
    }
    

    V3 : takes 1.98 sec (note that the constructor improves the performance)

    for ($i=1; $i<1000000; $i++) {
      $a = get_one();
      $b = $a->v;
      $b = $a->k;
    }
    
    function get_one() {
      return new test(1,1);
    }
    
    class test {
      public $v;
      public $k;
      public function __construct($v, $k) {
        $this->v = $v;
        $this->k = $k;
      }
    }
    
    0 讨论(0)
提交回复
热议问题