in reply to Re^2: looping efficiency
in thread looping efficiency
Don’t Optimize Code -- Benchmark It
-- from Ten Essential Development Practices by Damian Conway
The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming
-- Donald Knuth
Without good design, good algorithms, and complete understanding of the program's operation, your carefully optimized code will amount to one of mankind's least fruitful creations -- a fast slow program.
Wow, I assumed they were complaining about clarity rather than efficiency! :) For cheap thrills, assuming all you need are the file names 0000 .. 9999 in order, I benchmarked the three offered solutions as shown below.
use strict; use warnings; use Benchmark qw(timethese); sub orig { for my $i ( 0 .. 9 ) { for my $j ( 0 .. 9 ) { for my $k ( 0 .. 9 ) { for my $l ( 0 .. 9 ) { my $filename = "$i$j$k$l"; } } } } } sub yourmum { for ( 0 .. 9999 ) { my $filename = sprintf "%04d", $_; } } sub marshall { for ('0000' ... '9999') { my $filename = $_; } } orig(); yourmum(); marshall(); timethese 50000, { Orig => sub { orig() }, YourMum => sub { yourmum() }, Marshall => sub { marshall() }, };
On my machine, the results were:
Benchmark: timing 50000 iterations of Marshall, Orig, YourMum... Marshall: 25 wallclock secs (25.16 usr + 0.00 sys = 25.16 CPU) @ 19 +87.52/s (n=50000) Orig: 39 wallclock secs (38.83 usr + 0.00 sys = 38.83 CPU) @ 12 +87.73/s (n=50000) YourMum: 40 wallclock secs (40.08 usr + 0.00 sys = 40.08 CPU) @ 12 +47.57/s (n=50000)
If all you need are the file names (not the individual digits), it's no surprise that Marshall's suggestion was the fastest. I also think it is the simplest and clearest if all you need are the file names.
Update: see also perlperf - Perl Performance and Optimization Techniques. Added this node to Performance References.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: looping efficiency
by LanX (Saint) on Dec 30, 2020 at 03:03 UTC | |
by eyepopslikeamosquito (Archbishop) on Dec 30, 2020 at 03:36 UTC | |
Re^4: looping efficiency
by afoken (Chancellor) on Dec 31, 2020 at 13:45 UTC | |
by eyepopslikeamosquito (Archbishop) on Dec 31, 2020 at 20:46 UTC | |
Re^4: looping efficiency
by Bod (Parson) on Dec 30, 2020 at 12:01 UTC | |
Re^4: looping efficiency
by Anonymous Monk on Dec 30, 2020 at 03:51 UTC |