The only improvement I can see to your regex is to use non-capturing brackets for the outer set which speeds the process up very slightly. However, as [] indicates above, using split and limiting the captures is quicker (in my benchmark considerably so).

The only way of doing this faster (that I thought to try) is using index and substr, which to my suprise turns out to be quicker still. The latter method is labelled as "scanem" in the results below.

Benchmark: timing 10 iterations of , regexem, scanem, splitem ... regexem: 2 wallclock secs ( 1.86 usr + 0.00 sys = 1.86 CPU) @ 5 +.37/s (n=10), scanem: 1 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12 +.48/s (n=10), splitem: 1 wallclock secs ( 1.20 usr + 0.00 sys = 1.20 CPU) @ 8 +.32/s (n=10), Rate regexem splitem scanem regexem 5.37/s -- -35% -57% splitem 8.32/s 55% -- -33% scanem 12.5/s 133% 50% -- 2000, 2000, 2000 c:\test>

Some quick math shows that your lines average around 2000 chars, and I opted to generate test data with random lengths of between 5 and 12 chars (for approx 200 fields per line) and use a space as the separator.

The benchmark pushes every field, extracted one at a time from the test data, onto an array. These arrays are compared at the end to ensure identical results.

Benchmark code

#! perl -slw use strict; use vars qw/$test @splits @regexs @scans/; use Benchmark qw/cmpthese/; local $, = ', '; sub gendata { my ($min, $max) = @_; my $count = 0; my $data = ''; $data .= $count++ . ('X' x ($min + rand( $max-$min))) . ' ' while +length $data <2000; return $data; } sub splitem (\$$) { my ($scalar, $fieldno) = @_; return (split ' ', $$scalar, $fieldno+1)[-2]; } sub regexem (\$$) { my ($scalar, $fieldno) = @_; my ($result) = $$scalar =~ /(?:([^ ]*) ){$fieldno}/; return $result; } sub scanem (\$$) { my ($scalar, $fieldno) = @_; my $p = 0; $p = index( $$scalar, ' ', ++$p ) while --$fieldno >0; $p++ if $p; return substr $$scalar, $p, index($$scalar, ' ', $p)-$p; } $test = gendata 4, 10; cmpthese( 10, { splitem => 'push @splits, splitem $test, $_ for 1 .. 200', regexem => 'push @regexs, regexem $test, $_ for 1 .. 200', scanem => 'push @scans, scanem $test, $_ for 1 .. 200', }); print ~~@splits, ~~@regexs, ~~@scans; print 'splits differ' if "@splits" ne "@regexs" and "@regexs" eq "@sca +ns"; print 'regexs differ' if "@splits" ne "@regexs" and "@splits" eq "@sca +ns"; print 'scans differ' if "@scans" ne "@regexs" and "@splits" eq "@reg +exs";

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: What is the fastest way to extract data from a delimited string? by BrowserUk
in thread What is the fastest way to extract data from a delimited string? by thezip

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.