#!/usr/bin/env perl use warnings; use strict; use Time::HiRes qw( time ); use Devel::Peek; 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 (1 + int (rand() * 10)); if (rand() < 0.163) { $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; my $i1 = 0; while(<$fh>) { /^ ?Query/ && $match_count1 ++; if (/^ Query/) { Dump $_; $i1 ++; last if $i1 > 5; } } printf "%f read lines from disk and do RE ($match_count1 matches).\n", time - $time; seek $fh, 0, 0; my $s = ""; while(<$fh>) { $s .= $_; } $fh->close; #Dump $s; print "\n\n"; open $fh, "<", \$s; $time = time; my $match_count2; my $i2 = 0; while(<$fh>) { /^ ?Query/ && $match_count2++; if (/^ Query/) { Dump $_; $i2++; last if $i2 > 5; } } printf "%f read lines from in-memory file and do RE ($match_count2 matches).\n", time - $time;