The following is one example for two ways:
$arrayOfNumbers = array();
for($i=1; $i<= 10000; ++$i) {
$arrayOfNumbers[] = rand(1, 99999);
}
$maxHeap = new
SplMaxHeap ();
#first way
# Avg Time: 0.92856907844543s
array_map(array($maxHeap, 'insert'), $arrayOfNumbers);
#second way
# Avg Time: 1.3148670101166
foreach($arrayOfNumbers as $number) {
$maxHeap->insert($number);
}
It is due to the difference between Callback functions and normal functions.
In the second one, iteration of array using foreach, each iteration calls "insert" function and wait for the execution (function return control) and proceed to next iteration.
But in the array_map function, "insert" happens as callback function, it calls "insert" and don't wait for the result and call insert with next item in the array. So it is faster.
http://www.myresearch.biz/download/IJITCC2012.pdf