Update 1: Added walk2 by pushing to stack.
Update 2: The string length now obtained inside C.
Update 3: Changed from for loop to while loop.

Ah, I tried Inline::C. Walk1 runs faster than split in older Perl <= 5.20.x.

... use Inline 'C' => <<'END_C'; #include <stdlib.h> #include <string.h> SV * c_walk1(SV *sv) { STRLEN len; const char *s = SvPV_const(sv, len); const char *m = s, *strend = s + len; AV *ret = newAV(); while (s < strend) { if (*s++ == '\n') { av_push(ret, newSVpvn(m, s - m)); m = s; } } return newRV_noinc((SV *) ret); } void c_walk2(SV *sv) { STRLEN len; const char *s = SvPV_const(sv, len); const char *m = s, *strend = s + len; Inline_Stack_Vars; Inline_Stack_Reset; while (s < strend) { if (*s++ == '\n') { Inline_Stack_Push(sv_2mortal(newSVpvn(m, s - m))); m = s; } } Inline_Stack_Done; } END_C # walk1 - receive array ref { my $string = $huge_string; my $start = time; my $lines = c_walk1($string); printf "duration walk1: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@$lines); } # walk2 - receive list { my $string = $huge_string; my $start = time; my @lines = c_walk2($string); printf "duration walk2: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); }

Output from Perl 5.20.3 and Perl 5.26.1

$ /opt/perl-5.20.3/bin/perl demo.pl duration memfh: 0.526 seconds 917504 lines duration regex: 0.448 seconds 917504 lines duration split: 0.110 seconds 917504 lines duration trim : 0.268 seconds 917504 lines duration walk1: 0.073 seconds 917504 lines duration walk2: 0.110 seconds 917504 lines
$ /opt/perl-5.26.1/bin/perl demo.pl duration memfh: 0.479 seconds 917504 lines duration regex: 0.442 seconds 917504 lines duration split: 0.066 seconds 917504 lines duration trim : 0.259 seconds 917504 lines duration walk1: 0.070 seconds 917504 lines duration walk2: 0.084 seconds 917504 lines

Regards, Mario


In reply to Re^2: Fastest split possible by marioroy
in thread Fastest split possible by pedrete

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.