Skip to content

Instantly share code, notes, and snippets.

@phcostabh
Created December 10, 2013 12:02
Show Gist options
  • Save phcostabh/7889575 to your computer and use it in GitHub Desktop.
Save phcostabh/7889575 to your computer and use it in GitHub Desktop.
<?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