Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I suspect the redundancy and memory requirements in your one-liner is inefficient and that a while loop would be not only more efficient but more readable. Benchmark in progress ...

Update:After reviewing the original, its not exactly the same, but I think still a fair enough comparision :)

#!/usr/bin/perl -w # About the fairest comparison I could think of at the moment use strict; use Benchmark; my $file = "tst1.txt"; open(OUT, ">$file") or die "Can't open $file for output: $!"; for ("000000".."020000") { print OUT "$_=abc|def|ghi|jkl\n"; print OUT "Ignore this line\n"; } open(FH1, $file) or die "1 Can't open $file: $!"; open(FH2, $file) or die "2 Can't open $file: $!"; timethese(1, { MAPIT=>\&map_it, LOOPIT=>\&loop_it, }); close FH1; close FH2; sub map_it { my %author = map {/^\d{6}$/ ? $_ : [ split (/\|/, $_, 2 ]} map { split( /=/, $_, 2 ) } grep { /^\d{6}=[^|]+\|/ } <FH1>; } sub loop_it { my %author; while (<FH2>) { next unless /^(\d{6})=(.+)\|/; # Hmm, the above should really be: # next unless /^(\d{6})=([^|].*)\|/; # Whoops, forgot to limit the split to 2 elements here # No big deal, results are still similar $author{$1} = [ split(/\|/, $2) ]; } } # interesting different results #Under activestate perl Benchmark: timing 1 iterations of LOOPIT, MAPIT... LOOPIT: 5 wallclock secs ( 4.62 usr + 0.00 sys = 4.62 CPU) @ 0 +.22/s (n= ) (warning: too few iterations for a reliable count) MAPIT: 28 wallclock secs (28.73 usr + 0.00 sys = 28.73 CPU) @ 0 +.03/s (n= ) (warning: too few iterations for a reliable count) #under Cygwin perl $ ./tst Benchmark: timing 1 iterations of LOOPIT, MAPIT... LOOPIT: 4 wallclock secs ( 4.67 usr + 0.00 sys = 4.67 CPU) @ 0 +.21/s (n= ) (warning: too few iterations for a reliable count) MAPIT: 5 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 0 +.19/s (n= ) (warning: too few iterations for a reliable count)

In reply to Re: (Ovid - accidental obfuscation?)Re: Perverse Unreadable Code by runrig
in thread Perverse Unreadable Code by Anonymous Monk

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!
  • 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 2015-07-08 05:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (94 votes), past polls