<?php
$amount = $qty = $code = $id = range(1, 5000);
//dry run to cache function calls
$op = '%s executed in %.8f seconds';
microtime(true);
$max = count($id);
$start = $end = 0;
$data = [];
for ($i=0; $i<$max; $i++) {
$data[] = [
'id' => $id[$i],
'code' => $code[$i],
'qty' => $qty[$i],
'amount' => $amount[$i]
];
}
$data = [];
foreach($id as $i => $v) {
$data[$i] = [
'id' => $v,
'code' => $code[$i],
'qty' => $qty[$i],
'amount' => $amount[$i]
];
}
$data = array_map(function($a, $b, $c, $d) {
return [
'id' => $a,
'code' => $b,
'qty' => $c,
'amount' => $d
];
}, $id, $code, $qty, $amount);
//end dry-run
$start = microtime(true);
$data = [];
foreach($id as $i => $v) {
$data[] = [
'id' => $v,
'code' => $code[$i],
'qty' => $qty[$i],
'amount' => $amount[$i]
];
}
$end = microtime(true);
printf($op, 'foreach', $end - $start);
echo PHP_EOL;
//----
$start = microtime(true);
$max = count($id);
$data = [];
for ($i=0; $i<$max; $i++) {
$data[] = [
'id' => $id[$i],
'code' => $code[$i],
'qty' => $qty[$i],
'amount' => $amount[$i]
];
}
$end = microtime(true);
printf($op, 'for', $end - $start);
echo PHP_EOL;
$start = microtime(true);
$data = array_map(function($a, $b, $c, $d) {
return [
'id' => $a,
'code' => $b,
'qty' => $c,
'amount' => $d
];
}, $id, $code, $qty, $amount);
$end = microtime(true);
printf($op, 'array_map', $end - $start);
preferences:
51.32 ms | 407 KiB | 5 Q