Update: Added regex example.

Hello all :) I tried trimming the left side of the string. Plus incorporated the memfh example by davido. I'm curious too.

Split improves ~ 2x faster for this demonstration with Perl 5.22.x and later releases. That is the case on macOS.

use strict; use warnings; use Time::HiRes 'time'; my $huge_string = "aaa bbb\nccc ddd\neee fff\nggg hhh\niii jjj\nkkk ll +l\nmmm nnn\n"; # concatenate string exponentially to 917,504 lines $huge_string .= $huge_string for 1..17; # memfh { my $string = $huge_string; my $start = time; open my $memfh, '<', \$string; my @lines = <$memfh>; close $memfh; printf "duration memfh: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); } # regex { my $string = $huge_string; my $start = time; my @lines; while ( $string =~ /([^\n]+\n)/mg ) { my $line = $1; # save $1 to not lose the value push @lines, $line; } printf "duration regex: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); } # split { my $string = $huge_string; my $start = time; my @lines = split(/\n/, $string); printf "duration split: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); } # trim { my $string = $huge_string; my $start = time; my @lines; while ( my $line = substr($string, 0, index($string, "\n") + 1, '' +) ) { push @lines, $line; } printf "duration trim : %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); }

Output - Perl 5.28.2

duration memfh: 0.384 seconds 917504 lines duration regex: 0.387 seconds 917504 lines duration split: 0.067 seconds 917504 lines duration trim : 0.201 seconds 917504 lines

Another machine - Perl 5.26.1

duration memfh: 0.477 seconds 917504 lines duration regex: 0.445 seconds 917504 lines duration split: 0.065 seconds 917504 lines duration trim : 0.259 seconds 917504 lines

Same machine - Perl 5.18.2

duration memfh: 0.530 seconds 917504 lines duration regex: 0.490 seconds 917504 lines duration split: 0.130 seconds 917504 lines duration trim : 0.261 seconds 917504 lines

Regards, Mario


In reply to Re: 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.