Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
use strict; use warnings; use Benchmark (); sub t_for_r { my ($url, $list) = @_; my @return; push(@return, "$url/$_\n") foreach @$list; return @return; } sub t_map_r { my ($url, $list) = @_; my @return = map { "$url/$_\n" } @$list; return @return; } sub t_map2_r { my ($url, $list) = @_; return map { "$url/$_\n" } @$list; } sub t_for_a { my $url = shift; my @return; push(@return, "$url/$_\n") foreach @_; return @return; } sub t_map_a { my $url = shift; my @return = map { "$url/$_\n" } @_; return @return; } sub t_map2_a { my $url = shift; return map { "$url/$_\n" } @_; } { my $url = 'http://www.domain.com/'; my @list = qw( file0 file1 file2 file3 file4 file5 file6 file7 file8 file9 ); Benchmark::cmpthese(-3, { t_for_r => sub { $a = join('', t_for_r ($url, \@list)); }, t_map_r => sub { $a = join('', t_map_r ($url, \@list)); }, t_map2_r => sub { $a = join('', t_map2_r($url, \@list)); }, t_for_a => sub { $a = join('', t_for_a ($url, @list)); }, t_map_a => sub { $a = join('', t_map_a ($url, @list)); }, t_map2_a => sub { $a = join('', t_map2_a($url, @list)); }, }); } __END__ Rate t_for_r t_map_r t_for_a t_map_a t_map2_r t_map2_a t_for_r 20641/s -- -3% -5% -5% -33% -36% t_map_r 21196/s 3% -- -2% -2% -31% -34% t_for_a 21651/s 5% 2% -- -0% -29% -33% t_map_a 21684/s 5% 2% 0% -- -29% -32% t_map2_r 30598/s 48% 44% 41% 41% -- -5% t_map2_a 32087/s 55% 51% 48% 48% 5% --

At least for small lists, t_for and t_map are the same, and t_map2 is much faster.

Passing by ref vs passing the array doesn't matter if you use @_ directly.


In reply to Re^3: Turning foreach into map? by ikegami
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 having a coffee break in the Monastery: (5)
As of 2024-03-28 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found