<?php $refObjectTime = []; $objectTime = []; $copyArrayTime = []; $refArrayTime = []; function avg($array) { return empty($array) ? 0 : (array_sum($array) / count($array)); } function fooRefObject(&$var) { $var->test = 'bar'; } function fooObject($var) { $var->test = 'bar'; } function fooCopyArray($var) { $var['test'] = 'bar'; } function fooRefArray(&$var) { $var['test'] = 'bar'; } for ($i=0; $i<10000; ++$i) { //Passing object through reference, explicitly $start = microtime(true); $data = new stdclass; fooRefObject($data); $refObjectTime[] = microtime(true) - $start; //Passing object through reference, implicitly $start = microtime(true); $data = new stdclass; fooObject($data); $objectTime[] = microtime(true) - $start; //Passing array through copy-on-write $start = microtime(true); $data = []; fooCopyArray($data); $copyArrayTime[] = microtime(true) - $start; //Passing array through reference $start = microtime(true); $data = []; fooRefArray($data); $refArrayTime[] = microtime(true) - $start; } //Results: echo 'Results:' . PHP_EOL; echo 'Minimums:' . PHP_EOL . ' - Object through reference, explicitly: ' . min($refObjectTime) . PHP_EOL . ' - Object through reference, implicitly: ' . min($objectTime) . PHP_EOL . ' - Array through copy-on-write: ' . min($copyArrayTime) . PHP_EOL . ' - Array through reference: ' . min($refArrayTime) . PHP_EOL; echo 'Maximums:' . PHP_EOL . ' - Object through reference, explicitly: ' . max($refObjectTime) . PHP_EOL . ' - Object through reference, implicitly: ' . max($objectTime) . PHP_EOL . ' - Array through copy-on-write: ' . max($copyArrayTime) . PHP_EOL . ' - Array through reference: ' . max($refArrayTime) . PHP_EOL; echo 'Avergages:' . PHP_EOL . ' - Object through reference, explicitly: ' . avg($refObjectTime) . PHP_EOL . ' - Object through reference, implicitly: ' . avg($objectTime) . PHP_EOL . ' - Array through copy-on-write: ' . avg($copyArrayTime) . PHP_EOL . ' - Array through reference: ' . avg($refArrayTime) . PHP_EOL;
You have javascript disabled. You will not be able to edit any code.