#!/usr/bin/perl
use strict; use warnings;
use Time::HiRes qw(time);

# fake test data

my @numbers =  ((765432, 567890, 232323) x 500, 123456);
my $input = join '', map "this line $numbers[rand @numbers]\n", 1..2000000;
print length $input, " is length\n";
print $input =~ tr/\n//, ' lines and ', scalar(() = $input =~ /123456\n/g), " matches\n";

# end of fake stuff

my $start = time;
my $counter = my $counter2 = 0;
my $bufsize = 1024 * 10000;
my $bufcnt = 0;
open my $fh, '<', \$input or die "$! on open"; # needs adjusting
while( read $fh, $_, $bufsize )
  {
  $bufcnt++;
  $_ .= <$fh> // ''; # finish partial line
  $counter += tr/\n//;
  $counter2 += () = /123456\n/g;
  }
print "$counter lines and $counter2 matches in $bufcnt blocks of $bufsize in @{[time - $start]}\n";