Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Thanks to all who contrbuted to this thread. I have put together a quick benchmark. Unless I made an error in my code, map is the clear winner on performance grounds, as well as confusing any newbie that has to look at my code grounds ;-)

Rate or delete do copy slice map or delete 1620746/s -- -1% -50% -71% -78% do 1642036/s 1% -- -49% -70% -78% copy 3215434/s 98% 96% -- -42% -56% slice 5555556/s 243% 238% 73% -- -24% map 7299270/s 350% 345% 127% 31% --
Here is the code:
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); # create a hash with a few values deviating my $t_count = 3; my %hosts = map { $_ => $t_count - ( rand 50 > 49 ? 1 : 0 ) } (1 .. 1 +000); my $count = 10_000_000; cmpthese($count, { ' do' => ' do {delete $hosts{$_} unless $hosts{$_} == $t_co +unt} for keys %hosts ', ' map' => ' map {delete $hosts{$_} unless $hosts{$_} == $t_co +unt} keys %hosts ', 'or delete' => ' $hosts{$_} == $t_count or delete $hosts{$_} for k +eys %hosts ', ' slice' => ' delete @hosts{ grep { $hosts{$_} != $t_count } ke +ys %hosts } ', ' copy' => ' %hosts = map { $hosts{$_} == $t_count ? ($_, $hos +ts{$_}) : ()} keys %hosts ', });

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: map {} list or do {} for list? - Benchmarks by Random_Walk
in thread map {} list or do {} for list? by Random_Walk

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



  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (9)
    As of 2020-11-28 11:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?