Size 10........ Did method1 in 7.70092010498E-5 seconds Did method2 in 3.69548797607E-5 seconds Did method3 in 2.09808349609E-5 seconds Did method4 in 1.12056732178E-5 seconds Size 100........ Did method1 in 0.00318813323975 seconds Did method2 in 0.0011248588562 seconds Did method3 in 0.000842094421387 seconds Did method4 in 7.79628753662E-5 seconds Size 1000........ Did method1 in 0.301738977432 seconds Did method2 in 0.195058107376 seconds Did method3 in 0.0799751281738 seconds Did method4 in 0.000767946243286 seconds Size 10000........ Did method1 in 43.3766629696 seconds Did method2 in 74.302628994 seconds Did method3 in 10.0401690006 seconds Did method4 in 0.0124380588531 seconds Size 10........ Did method1 in 7.70092010498E-5 seconds Did method2 in 3.69548797607E-5 seconds Did method3 in 2.09808349609E-5 seconds Did method4 in 1.12056732178E-5 seconds Size 100........ Did method1 in 0.00318813323975 seconds Did method2 in 0.0011248588562 seconds Did method3 in 0.000842094421387 seconds Did method4 in 7.79628753662E-5 seconds Size 1000........ Did method1 in 0.301738977432 seconds Did method2 in 0.195058107376 seconds Did method3 in 0.0799751281738 seconds Did method4 in 0.000767946243286 seconds Size 10000........ Did method1 in 43.3766629696 seconds Did method2 in 74.302628994 seconds Did method3 in 10.0401690006 seconds Did method4 in 0.0124380588531 seconds "; // generate an array of indexs to test, we'll have size/2..... $randomIndexes = array(); for ($i = 0; $i < $size/2; ++$i) { $randomIndexes[] = rand(0, $size-1); } $time_start = microtime(true); foreach($randomIndexes as &$idx) { getByIndexMethod1($myArray, $idx); } $time_end = microtime(true); $time = $time_end - $time_start; echo "Did method1 in {$time} seconds
"; $time_start = microtime(true); foreach($randomIndexes as &$idx) { getByIndexMethod2($myArray, $idx); } $time_end = microtime(true); $time = $time_end - $time_start; echo "Did method2 in {$time} seconds
"; $time_start = microtime(true); foreach($randomIndexes as &$idx) { getByIndexMethod3($myArray, $idx); } $time_end = microtime(true); $time = $time_end - $time_start; echo "Did method3 in {$time} seconds
"; $time_start = microtime(true); $stuff = array_values($myArray); foreach($randomIndexes as &$idx) { getByIndexMethod4($myArray, $idx, $stuff); } $time_end = microtime(true); $time = $time_end - $time_start; echo "Did method4 in {$time} seconds

"; flush(); } function getByIndexMethod1(&$array, $index) { reset($array); $idx = 0; $ret =& current($array); do { if ($idx == $index) { return $ret; } ++$idx; } while ($ret =& next($array)); return false; } function getByIndexMethod2(&$array, $index) { $tmp = array_values($array); if (isset($tmp[$index])) return $tmp[$index]; return false; } function getByIndexMethod4(&$array, $index, &$tmp) { if (isset($tmp[$index])) return $tmp[$index]; return false; } function getByIndexMethod3(&$array, $index) { $i = 0; foreach($array as $key => &$val) { if ($index == $i) return $val; $i++; } return false; }