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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi,
some days ago I faced the time taken by unpack compared to other ways of extracting fields from records.
I think that using substr can speed up the things.
After your posting I took the occasion to learn how to use Benchmark and tested this code below on a 400000-record file:
#/usrl/bin/perl -w use Benchmark; $filename = @ARGV[0]; timethese ( $count, {'Method One' => '&One', 'Method Two' => '&Two', 'Method Three' => '&Three'} ); sub One { open(FILE,@ARGV[0]); while($row=<FILE>) { @data = unpack('a4a2a2a2a2',$row); } close(FILE); } sub Two { open(FILE,@ARGV[0]); while($row=<FILE>) { ($data[0],$data[1],$data[2],$data[3],$data[4]) = unpack('a4a2a2a2a2',$row); } close(FILE); } sub Three { open(FILE,@ARGV[0]); while($row=<FILE>) { $data[0] = substr($row,0,4); $data[1] = substr($row,4,2); $data[2] = substr($row,6,2); $data[3] = substr($row,8,2); $data[4] = substr($row,10,2); } close(FILE); }
and I got this.
Method One: 43 wallclock secs (40.72 usr + 1.23 sys = 41.95 CPU) @ 0 +.02/s (n=1) (warning: too few iterations for a reliable count) Method Two: 43 wallclock secs (41.50 usr + 1.42 sys = 42.92 CPU) @ 0 +.02/s (n=1) (warning: too few iterations for a reliable count) Method Three: 36 wallclock secs (33.76 usr + 1.42 sys = 35.18 CPU) @ + 0.03/s (n=1) (warning: too few iterations for a reliable count)
Again I think sub Three approach (substr) proves to be faster than sub One (unpack into an array) or Two (unpack into array elements).
Hope this may help.

ciao,
Roberto

In reply to Re: Assining data to an array is slow.. by rbi
in thread Assining data to an array is slow.. by smferris

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?
    choroba still has one more.
    [Corion]: I have Thursday and Friday off, so then I might get to converting the rough outlines to more articles ;)
    [Corion]: But currently, most of the modules are web-related and I don't like to publish two web articles in a row
    [Corion]: Maybe I should do the Filter::Simple release on the next weeked - this would give me one more article to milk from this theme

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (8)
    As of 2017-04-24 08:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I'm a fool:











      Results (437 votes). Check out past polls.