3 ms·
I don't know PHP, but feel free to send a pull request!
by kmod 6y ago
I don't know PHP, but feel free to send a pull request!
- owyn 6y agoUsing range() in php it takes about 1.3 seconds. The PHP docs imply that it's a generator now but I'm not positive about that. I wrote an equivalent php function just using for loops and calling strval($x) 20 million times and on my laptop it runs in .9 seconds... The second form of creating 20 lists with 1m elements runs in .4 seconds. Without needing to write 300 lines of Py/C stuff. So... shrug? microbenchmarks? I guess writing the optimized code was the fun part and the actual benchmark/timing part doesn't really matter. It's just for loops... the other comments are right that benchmarks definitely matter when you're doing more varied "work" though. For 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; } } }