Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

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

Thanks for reply, BrowserUK.

I tried and below is the result.

          s/iter 02_split1 04_unpack 03_split2 01_substr
02_split1   6.34        --      -34%      -41%      -57%
04_unpack   4.17       52%        --      -11%      -35%
03_split2   3.71       71%       12%        --      -27%
01_substr   2.70      134%       54%       37%        --
And test code. I hope there is no silly mistakes.
#!/usr/bin/perl use strict; use warnings; use Time::HiRes; use Benchmark qw/cmpthese/; my $href; sub test1{ $href={}; open(my $fh, "<", "04.txt") or die $!; while(<$fh>){ chomp; push @{ $href->{ substr($_,0,10)} }, [ substr($_,10,10), subst +r($_,20)]; } close $fh; } sub test2{ my @rec; $href={}; open(my $fh, "<", "04.txt") or die $!; push @{ $href->{ $rec[0] } }, [ @rec[ 1, 2 ] ] while @rec = split '(?<=-[a-z])', <$fh>; close $fh; } sub test3{ #04-1.txt, with delimiter '|' my @rec; $href={}; open(my $fh, "<", "04-1.txt") or die $!; push @{ $href->{ $rec[0]} }, [ @rec[1, 2] ] while @rec = split /\|/, <$fh>; close $fh; } sub test4{ #with unpack my @rec; $href={}; open(my $fh, "<", "04.txt") or die $!; @rec = unpack( 'a10a10a4', $_ ), push @{ $href->{ $rec[0] } }, [ @rec[ 1, 2 ] ] while <$fh>; close $fh; } my %tests = ( '01_substr' => \&test1, '02_split1' => \&test2, '03_split2' => \&test3, '04_unpack' => \&test4, ); cmpthese( -20, #for 20 cpu secs \%tests );
I thought, seeing your unpack example, if there is a way like this ? This is impossible because unpack returns flat list, though...
open(my $fh, "<", "" ) or die $!; local $/ = undef; map { push @{ $hash{ $_->[0] } }, [ $_->[1], $_->[2] ] } unpack( '(a10a10a4)*', <$fh>), close $fh;
With large loop, setting value to variable becomes some cost( this is BrowserUK taught me in this thread). So I think if I can avoid to use @rec, unpack and split becomes faster. Is there a good way?

In reply to Re^2: Loading 283600 records (Updated) by remiah
in thread Loading 283600 records (WordNet) by remiah

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2017-12-17 04:52 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (462 votes). Check out past polls.