It could be related to your system or to the cygwin build or its general environment.

Running with Strawberry Perl 5.38 I see the in-memory version being faster. Results are similar for SP 5.36.1.

perl 11157166.pl tempfile.txt is size 29 Mb 0.133742 read lines from disk and do RE (2047 matches). 0.079146 read lines from in-memory file and do RE (2047 matches).

Edit: Tried with MSYS2 and results are slower for the in-memory loop.

tempfile.txt is size 29 Mb 0.057704 read lines from disk and do RE (2047 matches). 0.805546 read lines from in-memory file and do RE (2047 matches).

(End of edit)

Modified code is below. Main change is to generate the file if needed (beware lack of overwrite safety). It also reports the number of matches.

#!/usr/bin/env perl use warnings; use strict; use Time::HiRes qw( time ); my $file = shift @ARGV; my ($fh, $time); if (!$file) { # should use Path::Tiny::tempfile $file = 'tempfile.txt'; open my $ofh, '>', $file or die "Cannot open $file for writing, $! +"; srand(1234567); for my $i (0..200000) { my $string = 'some random text ' . rand(); $string = $string x int (rand() * 10); if (rand() < 0.01) { $string = " Query${string}"; } say {$ofh} $string; } $ofh->close or die "Cannot close $file, $!"; printf "%s is size %i Mb\n", $file, (-s $file) / (1028**2); } open $fh, "<", $file; $time = time; my $match_count1; while(<$fh>) { /^ ?Query/ && $match_count1++; } printf "%f read lines from disk and do RE ($match_count1 matches).\n", + time - $time; seek $fh, 0, 0; my $s = ""; while(<$fh>) { $s .= $_; } open $fh, "<", \$s; $time = time; my $match_count2; while(<$fh>) { /^ ?Query/ && $match_count2++;; } printf "%f read lines from in-memory file and do RE ($match_count2 mat +ches).\n", time - $time;

In reply to Re: RE on lines read from in-memory scalar is very slow by swl
in thread RE on lines read from in-memory scalar is very slow by Danny

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.