4 ms·
PHP $numbers = []; for ($i = -10000000; $i < 10000000; $i++) $numbers[] = $i; $lowest = PHP_INT_MAX; foreach ($numbers as $i) { if ($i
by tored 5y ago
PHP
$numbers = [];
for ($i = -10000000; $i < 10000000; $i++) $numbers[] = $i;
$lowest = PHP_INT_MAX;
foreach ($numbers as $i) {
if ($i > 0 && $i < $lowest) {
$lowest = $i;
}
}
1.08 seconds
PHP Optimized
$lowest = PHP_INT_MAX;
foreach (range(-10000000, 10000000) as $i) {
if ($i > 0 && $i < $lowest) {
$lowest = $i;
}
}
0.56 seconds
Make sure to enable JIT (tracing)
Node for loop version for me took 1.25 seconds (I have a very old rig)
php --version
PHP 8.0.11 (cli) (built: Sep 21 2021 18:25:57) ( NTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.11, Copyright (c) Zend Technologies
with Zend OPcache v8.0.11, Copyright (c), by Zend Technologies
node --version
v16.1.0
Note: I measured time within the script and not outside to avoid any bootup time.
- jeroenhd 5y agoAll of my measurements included bootup time but PHP took way longer. I guess I don't have JIT enabled, then, because the code is near identical.