- array_sum: documentation ( source)
- microtime: documentation ( source)
<?php
/**
* My results:
*
* Average time to process 10,000 items, 100 runs
*
* Array:: 0.0034920692443848
* Obj: 0.026956255435944
*/
/**
* Class foo
*/
class Foo {
/**
* @var
*/
protected $bar;
/**
* Builds it.
*
* @param mixed $bar
* Something bar
*/
public function __construct($bar) {
$this->bar = $bar;
}
/**
* Something.
*
* @return mixed
* Something
*/
public function getBar() {
return $this->bar;
}
}
$foo['bar'] = 'baz';
$baz = new Foo('baz');
$cycles = 10000;
$array = $obj = array();
for ($j = 0; $j < 100; $j++) {
$start = microtime(TRUE);
for ($i = 0; $i < $cycles; $i++) {
$b = $foo['bar'];
}
$array[] = microtime(TRUE) - $start;
$start = microtime(TRUE);
for ($i = 0; $i < $cycles; $i++) {
$b = $baz->getBar();
}
$obj[] = microtime(TRUE) - $start;
}
echo "Array: ";
echo array_sum($array) / count($array);
echo "\n\nObject: ";
echo array_sum($obj) / count($obj);