- microtime: documentation ( source)
- md5: documentation ( source)
<?php
class ArrayTester
{
const TEST_LOOP_SIZE = 1000000;
public function go()
{
$start = microtime(true);
for($a = 0; $a<=self::TEST_LOOP_SIZE;++$a) {
$test = $this->getTest();
$someVal = $test[$a];
}
echo 'Pass by copy: ' . (microtime(true) - $start) . ' sec<br>';
$start = microtime(true);
for($a = 0; $a<=self::TEST_LOOP_SIZE;++$a) {
$test = &$this->getTestByRef();
$someVal = $test[$a];
}
echo 'Pass by reference: ' . (microtime(true) - $start) . ' sec<br>';
}
private $_test;
private function getTest() {
if(!$this->_test) {
for($a = 0; $a<=self::TEST_LOOP_SIZE;++$a) {
$this->_test[$a]=md5($a);
}
}
return $this->_test;
}
private $_testByRef;
private function &getTestByRef() {
if(!$this->_testByRef) {
for($a = 0; $a<=self::TEST_LOOP_SIZE;++$a) {
$this->_testByRef[$a]=md5($a);
}
}
return $this->_testByRef;
}
}
$tester = new ArrayTester();
$tester->go();