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

Re: (Ovid - accidental obfuscation?)Re: Perverse Unreadable Code

by runrig (Abbot)
on Apr 26, 2001 at 21:33 UTC ( #75834=note: print w/ replies, xml ) Need Help??


in reply to (Ovid - accidental obfuscation?)Re: Perverse Unreadable Code
in thread Perverse Unreadable Code

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)


Comment on Re: (Ovid - accidental obfuscation?)Re: Perverse Unreadable Code
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://75834]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2015-07-04 14:37 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 (60 votes), past polls