<?php
$a = array();
for($i = 0; $i < pow(10, 5); $i++) {
$a[$i] = array(
md5($i.microtime(true))
);
}
$sum = 0;
$times['for']['before'] = microtime(true);
for($j = 0; $i < count($a); $i++) {
$sum += 1000;
}
$times['for']['after'] = microtime(true);
unset($sum);
$sum = 0;
$times['foreach']['before'] = microtime(true);
foreach($a as $k) {
$sum += 1000;
}
$times['foreach']['after'] = microtime(true);
unset($sum);
$t_for = $times['for']['after'] - $times['for']['before'];
$t_foreach = $times['foreach']['after'] - $times['foreach']['before'];
var_dump(array(
'for' => sprintf('%f', $t_for),
'foreach' => sprintf('%f', $t_foreach),
'is \'for\' faster than \'foreach\'?' => $t_for < $t_foreach,
'how much faster?' => "".(1 - ($t_for / $t_foreach)) * 100).'%'
);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.13, 7.3.0 - 7.3.1
- Notice: Array to string conversion in /in/T2mPp on line 33
string(6) "Array%"
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
- Notice: Array to string conversion in /in/T2mPp on line 34
string(6) "Array%"
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- string(6) "Array%"
preferences:
202.38 ms | 402 KiB | 288 Q