I decided to test a substr approach. It is almost as fast as the unpack approach, but not quite.
Benchmark: running Regex, Substr, Unpack, each for at least 3 CPU seco +nds... Regex: 2 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 42 +172.56/s (n=133687) Substr: 2 wallclock secs ( 3.12 usr + 0.00 sys = 3.12 CPU) @ 56 +391.35/s (n=175941) Unpack: 2 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 61 +831.65/s (n=195388)
#!/usr/bin/perl -w use strict; use Benchmark; my $input = 'X' x 65; #test input my @result; timethese(0, { 'Regex' => sub { undef @result; @result = ($input =~ /(.{16})(.{20})(.{20})(.{1})(. +{8})/); # print join "\t", @result; }, 'Unpack' => sub { undef @result; @result = unpack "A16A20A20AA8",$input; # print join "\t", @result; }, 'Substr' => sub { undef @result; @result=((substr $input,0,15), (substr $input,15,20), (substr $input,35,20), (substr $input,55,1), (substr $input,56,8)); # print join "\t", @result; }, });

In reply to Re: Fixed-Length Fields Question by lhoward
in thread Fixed-Length Fields Question by mrmick

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, details, 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, summary, 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.