Thank you for your quick reply and pointing me in the direction of
Benchmark. Observant me didn't even think of time passed since the statement was made, but I figured since I asked the question I may as well go ahead and learn how to use Benchmark and get the answer. For anybody interested in the results:
For the sake of simplicity I used the code from the passage broken into subroutines to be called by Benchmark:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all);
my %h;
@h{'A'..'Z','a'..'z'} = 1..52;
sub verbose {
my $hash = shift;
foreach my $key (sort keys %$hash) {
print "$key: $hash->{$key}\n";
}
}
sub idiom {
my $hash = shift;
print map "$_: $hash->{$_}\n", sort keys %$hash;
}
timethese(100000, {
'Verbose' => 'verbose(\%h)',
'Idiom' => 'idiom(\%h)',
});
timethese(1000000, {
'Verbose' => 'verbose(\%h)',
'Idiom' => 'idiom(\%h)',
});
timethese(10000000, {
'Verbose' => 'verbose(\%h)',
'Idiom' => 'idiom(\%h)',
});
RESULTS:
Benchmark: timing 100000 iterations of Idiom, Verbose...
Idiom: 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1250000.00/s (n=100000)
(warning: too few iterations for a reliable count)
Verbose: 0 wallclock secs ( 0.12 usr + 0.00 sys = 0.12 CPU) @ 833333.33/s (n=100000)
(warning: too few iterations for a reliable count)
Benchmark: timing 1000000 iterations of Idiom, Verbose...
Idiom: 1 wallclock secs ( 0.76 usr + 0.00 sys = 0.76 CPU) @ 1315789.47/s (n=1000000)
Verbose: 1 wallclock secs ( 1.12 usr + 0.00 sys = 1.12 CPU) @ 892857.14/s (n=1000000)
Benchmark: timing 10000000 iterations of Idiom, Verbose...
Idiom: 8 wallclock secs ( 7.52 usr + 0.00 sys = 7.52 CPU) @ 1329787.23/s (n=10000000)
Verbose: 12 wallclock secs (11.76 usr + 0.00 sys = 11.76 CPU) @ 850340.14/s (n=10000000)
So at least for this particular piece of code map proves to be faster. Thanks again for helping me answer my own question. As always, I'm still a beginner so if this is misguided/messy code or i tested this wrong please let me know.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.