Created
          December 10, 2013 12:02 
        
      - 
      
 - 
        
Save phcostabh/7889575 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /** | |
| * Source from http://www.yiiframework.com/forum/index.php/topic/11568-php-objects-vs-arrays-performance-myth/ | |
| */ | |
| define('NUM_INSTANCES', 10); | |
| define('NUM_TESTS', 10000); | |
| class TestObject | |
| { | |
| public $a; | |
| public $b; | |
| public $c; | |
| public function __construct($a,$b,$c) | |
| { | |
| $this->a = $a; | |
| $this->b = $b; | |
| $this->c = $c; | |
| } | |
| } | |
| class Test | |
| { | |
| public function getObjects() | |
| { | |
| $a = array(); | |
| for ($i=0; $i<NUM_INSTANCES; $i++) | |
| $a[] = new TestObject('a','b','c'); | |
| return $a; | |
| } | |
| public function getArrays() | |
| { | |
| $a = array(); | |
| for ($i=0; $i<NUM_INSTANCES; $i++) | |
| $a[] = $this->buildArray('a', 'b', 'c'); | |
| return $a; | |
| } | |
| public function buildArray($a, $b, $c) | |
| { | |
| $array = array(); | |
| $array['a'] = $a; | |
| $array['b'] = $b; | |
| $array['c'] = $c; | |
| return $array; | |
| } | |
| public function useObject($o) | |
| { | |
| $a = $o->a; | |
| $b = $o->b; | |
| $o->c = 'xxx'; | |
| } | |
| public function useArray($o) | |
| { | |
| $a = $o['a']; | |
| $b = $o['b']; | |
| $o['c'] = 'xxx'; | |
| } | |
| } | |
| $test = new Test; | |
| // Benchmark with objects: | |
| $start = microtime(true); | |
| for ($i=0; $i<NUM_TESTS; $i++) | |
| { | |
| $x = $test->getObjects(); | |
| foreach ($x as $y) | |
| $test->useObject($y); | |
| } | |
| echo "\nObject time = " . (microtime(true)-$start) . "\n\n"; | |
| // Benchmark with arrays: | |
| $start = microtime(true); | |
| for ($i=0; $i<NUM_TESTS; $i++) | |
| { | |
| $x = $test->getArrays(); | |
| foreach ($x as $y) | |
| $test->useArray($y); | |
| } | |
| echo "\nArray time = " . (microtime(true)-$start) . "\n\n"; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment