I'd be interested in seeing how PHP performs running equivalent code.
foreach(range(1,20) as $j)
foreach(range(1,1000000) as $i)
strval($i);
Running on my low-end EC2 box: php7.3 py.php == 1.258s
python2 py.py == 3.714s
python3 py.py == 4.530sFor what it's worth, I did benchmark a big application in PHP "for real" and parsing a large configuration file (10,000 lines +) on every request did take up about %15 of the wall clock time. We optimized a few things there because it was worth it. It was a HUGE application of about ~1M LoC and an average request was about 300-400 milliseconds so... I guess it wasn't doing a lot of for loops?
Edit: I had a few minutes before my next meeting to code golf this so I decided to test if range() worked with yield [1]
<?
function gen() { // 1.3s
foreach (range(0,20) as $i) {
foreach (range(0,1000000) as $j) {
strval($j);
}
}
}
function foo() { // .9s
for ($i = 0; $i < 20; $i++) {
for ($j = 0; $j < 1000000; $j++) {
strval($j);
}
}
}
function bar() { // .4s
for ($i = 0; $i < 20; $i ++) {
$x = range(0, 1000000);
}
}
// [1] returns in .06 seconds
// pretty sure this just executes yield once and does no real work just like me this morning
function gen2() {
foreach (range(0,20) as $i) {
foreach (range(0,1000000) as $j) {
yield;
}
}
}