Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Foreach is faster than map if you're modifying in place. If you're using foreach to push into an array, like what's happening here, then map is going to be faster. (which is why there's the recommendation not to use map if you're not doing anything with the list generated -- you might as well have just done foreach).

Update: benchmarks to show the speed differences between map, map reassigning to the original list, map in void context, for, and foreach. (if 'recent' is before 5.8.6, I don't think the optimizations worked)

purple:/tmp oneiros$ perl -version | head -2 This is perl, v5.8.6 built for darwin-thread-multi-2level purple:/tmp oneiros$ perl timer.pl 100: Rate map new_map void_map for foreach map 1089/s -- -16% -37% -61% -61% new_map 1295/s 19% -- -25% -54% -54% void_map 1718/s 58% 33% -- -38% -39% for 2793/s 156% 116% 63% -- -1% foreach 2825/s 159% 118% 64% 1% -- 500: Rate map new_map void_map foreach for map 213/s -- -14% -35% -60% -60% new_map 249/s 17% -- -24% -53% -54% void_map 328/s 54% 32% -- -38% -39% foreach 526/s 147% 112% 61% -- -2% for 538/s 153% 116% 64% 2% -- 2500: Rate map new_map void_map for foreach map 38.5/s -- -15% -38% -61% -61% new_map 45.4/s 18% -- -27% -54% -55% void_map 61.9/s 61% 37% -- -38% -38% for 99.5/s 158% 119% 61% -- -0% foreach 100.0/s 160% 120% 61% 0% --
use warnings; use strict; use Benchmark qw(cmpthese); my @counts = qw( 100 500 2500 ); my %templates = ( map => '@list = map { s/1//; $_ } @list;', new_map => 'my @newlist = map { s/1//; $_ } @list;', void_map => 'map { s/1// } @list;', foreach => 's/1// foreach @list;', for => 's/1// for @list;' ); my @tests = (); while ( my ($name, $template) = each (%templates)) { my $test = <<EOF; \$test = sub { my \@list = \@_; $template } EOF eval $test; foreach my $count (@counts) { $tests[$count]->{$name} = sub { &$test( 1 .. $count ) +}; } } foreach my $count (@counts) { print "\n$count:\n"; cmpthese( 500_000/$count, $tests[$count] ); }

In reply to Re^2: Turning foreach into map? by jhourcle
in thread Turning foreach into map? by ghenry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-16 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found