PHP Objects vs Arrays — Performance comparison while iterating

前端 未结 10 1787
青春惊慌失措
青春惊慌失措 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:05

    Well I got curious today based off of @magallanes benchmark, so I expanded it a bit. I upped some of the for loops to really highlight the gaps between things. This is running on Apache 2.4, mod_php, and PHP 7.2.

    Here's a summary table to make the results easier:

    +---------------------------+---------+-----------------+
    |           Test            | Memory  |      Time       |
    +---------------------------+---------+-----------------+
    | Array                     | 2305848 | 9.5637300014496 |
    | stdClass                  | 2505824 | 11.212271928787 |
    | SomeClass                 |  963640 | 11.558017015457 | <-- *
    | AnotherClass              | 2563136 | 10.872401237488 |
    | SetterClass               |  905848 | 59.879059791565 |
    | SetterClassDefineReturn   |  905792 | 60.484427213669 |
    | SetterClassSetFromParam   |  745792 | 62.783381223679 |
    | SetterClassSetKeyAndParam |  745824 | 72.155715942383 |
    +---------------------------+---------+-----------------+
    * - Winner winner chicken dinner
    

    Below is the modified script. I wanted to test setting properties with methods and defining types. I was very surprised to find that using setter methods adds a significant hit to the code. Now granted this is a very very specific performance test where many apps will not even hit this. But if you have a site that handles 1000/reqs/second with 1000 classes that are used with 1000s of objects, you can see how this may affect performance.

    aaa = 'aaa';
        }
    
        public function setBBB() {
            $this->bbb = 'bbb';
        }
    
        public function setCCC() {
            $this->ccc = $this->aaa.$this->bbb;
        }
    }
    
    class SetterClassDefineReturn {
        public $aaa;
        public $bbb;
        public $ccc;
    
        public function setAAA():void {
            $this->aaa = 'aaa';
        }
    
        public function setBBB():void {
            $this->bbb = 'bbb';
        }
    
        public function setCCC():void {
            $this->ccc = $this->aaa.$this->bbb;
        }
    }
    
    class SetterClassSetFromParam {
        public $aaa;
        public $bbb;
        public $ccc;
    
        public function setAAA(string $val): void {
            $this->aaa = $val;
        }
    
        public function setBBB(string $val): void {
            $this->bbb = $val;
        }
    
        public function setCCC(string $val): void {
            $this->ccc = $val;
        }
    }
    
    class SetterClassSetKeyAndParam {
        public $aaa;
        public $bbb;
        public $ccc;
    
        public function set(string $key, string $val): void {
            $this->{$key} = $val;
        }
    }
    
    function p($i) {
      echo '
    ';
      print_r($i);
      echo '
    '; echo '
    '; } $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new SomeClass(); for ($j=0; $j<5000; $j++) { $z->aaa = 'aaa'; $z->bbb = 'bbb'; $z->ccc = $z->aaa.$z->bbb; } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new AnotherClass(); for ($j=0; $j<5000; $j++) { $z->aaa = 'aaa'; $z->bbb = 'bbb'; $z->ccc = $z->aaa.$z->bbb; } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new SetterClass(); for ($j=0; $j<5000; $j++) { $z->setAAA(); $z->setBBB(); $z->setCCC(); } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new SetterClassDefineReturn(); for ($j=0; $j<5000; $j++) { $z->setAAA(); $z->setBBB(); $z->setCCC(); } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new SetterClassSetFromParam(); for ($j=0; $j<5000; $j++) { $z->setAAA('aaa'); $z->setBBB('bbb'); $z->setCCC('aaabbb'); } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new SetterClassSetKeyAndParam(); for ($j=0; $j<5000; $j++) { $z->set('aaa', 'aaa'); $z->set('bbb', 'bbb'); $z->set('ccc', 'aaabbb'); } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = new stdClass(); for ($j=0; $j<5000; $j++) { $z->aaa = 'aaa'; $z->bbb = 'bbb'; $z->ccc = $z->aaa.$z->bbb; } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z); $t0 = microtime(true); $arraysOf=[]; $inicio=memory_get_usage(); for ($i=0; $i<5000; $i++) { $z = []; for ($j=0; $j<5000; $j++) { $z['aaa'] = 'aaa'; $z['bbb'] = 'bbb'; $z['ccc'] = $z['aaa'].$z['bbb']; } $arraysOf[]=$z; } $fin=memory_get_usage(); echo '

    Time Taken (seconds): '.(microtime(true) - $t0).'

    '; echo '

    Memory: '.($fin-$inicio).'

    '; p($z);

    And here's the results:

    Time Taken (seconds): 11.558017015457
    
    Memory: 963640
    
    SomeClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    -----
    
    Time Taken (seconds): 10.872401237488
    
    Memory: 2563136
    
    AnotherClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 59.879059791565
    
    Memory: 905848
    
    SetterClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 60.484427213669
    
    Memory: 905792
    
    SetterClassDefineReturn Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 62.783381223679
    
    Memory: 745792
    
    SetterClassSetFromParam Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 72.155715942383
    
    Memory: 745824
    
    SetterClassSetKeyAndParam Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 11.212271928787
    
    Memory: 2505824
    
    stdClass Object
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    ----
    
    Time Taken (seconds): 9.5637300014496
    
    Memory: 2305848
    
    Array
    (
        [aaa] => aaa
        [bbb] => bbb
        [ccc] => aaabbb
    )
    
    

提交回复
热议问题