fork download
  1. #!/usr/bin/perl
  2. use strict; use warnings;
  3. use Time::HiRes qw(time);
  4.  
  5. # fake test data
  6.  
  7. my @numbers = ((765432, 567890, 232323) x 500, 123456);
  8. my $input = join '', map "this line $numbers[rand @numbers]\n", 1..2000000;
  9. print length $input, " is length\n";
  10. print $input =~ tr/\n//, ' lines and ', scalar(() = $input =~ /123456\n/g), " matches\n";
  11.  
  12. # end of fake stuff
  13.  
  14. my $start = time;
  15. my $counter = my $counter2 = 0;
  16. my $bufsize = 1024 * 10000;
  17. my $bufcnt = 0;
  18. open my $fh, '<', \$input or die "$! on open"; # needs adjusting
  19. while( read $fh, $_, $bufsize )
  20. {
  21. $bufcnt++;
  22. $_ .= <$fh> // ''; # finish partial line
  23. $counter += tr/\n//;
  24. $counter2 += () = /123456\n/g;
  25. }
  26. print "$counter lines and $counter2 matches in $bufcnt blocks of $bufsize in @{[time - $start]}\n";
Success #stdin #stdout 3.28s 189056KB
stdin
Standard input is empty
stdout
34000000 is length
2000000 lines and 1362 matches
2000000 lines and 1362 matches in 4 blocks of 10240000 in 0.156703948974609