#!/opt/local/bin/perl use warnings; use strict; use Benchmark qw(:all); my $fh = *DATA; my $start = tell DATA; cmpthese(100000, { all => '_all', while => '_while', split => '_split', end_of_substring => '_end_of_substring', end_slurped => '_end_slurped', tr_and_match => '_tr_and_match', tr => '_tr', tr_moar_magic => '_tr_moar_magic', tr_and_split => '_tr_and_split', tr_and_split_redux => '_tr_and_split_redux', tr_and_end => '_tr_and_end', }); sub _all { seek $fh, $start, 0; my $count = 0; while (<$fh>){ $count += () = /(M+)/g; } # print "all: $count\n"; } sub _while { seek $fh, $start, 0; my $count=0; while(<$fh>){ while($_=~/(M+)/g){ $count++; } } # print "while: $count\n"; } sub _end_of_substring { seek $fh, $start, 0; my $count = 0; while ( <$fh> ) { $count += () = m/M[^M]/g; } # print "end_of_substring: $count\n"; } sub _end_slurped { seek $fh, $start, 0; local $/; $_ = <$fh>; my $count = () = m/M[^M]/g; # print "end_slurped: $count\n"; } sub _split { seek $fh, $start, 0; local $/; my $count = ( split /[^M]+/, <$fh> ) - 1; # print "split: $count\n"; } sub _tr { seek $fh, $start, 0; local $/; $_ = <$fh>; my $count = tr/M// =~ tr/M/M/sr; # print "tr: $count\n"; } sub _tr_moar_magic { seek $fh, $start, 0; local $/; $_ = <$fh>; tr/M/M/s; my $count = tr/M//; # print "tr: $count\n"; } sub _tr_and_split { seek $fh, $start, 0; local $/; $_ = <$fh>; tr/M/I/cs; my $count = (split /I/) - 1; # print "tr_and_split: $count\n"; } sub _tr_and_split_redux { seek $fh, $start, 0; local $/; $_ = <$fh>; tr/M/I/cs; tr/M/M/s; my $count = (split /I/) - 1; # print "tr_and_split_redux: $count\n"; } sub _tr_and_end { seek $fh, $start, 0; local $/; $_ = <$fh>; tr/M/I/cs; my $count = () = m/M[^M]/g; # print "tr_and_end: $count\n"; } sub _tr_and_match { seek $fh, $start, 0; local $/; $_ = <$fh>; $_ = tr/M/ /csr; my $count = () = m/(M)+/g; # print "tr_and_match: $count\n"; } __DATA__ IIIIIIIIIIIMMMMMMMMMMMMOOOOOOOOOOOOMMMMMMMMMIIIIIIIIIMM IIIIIIMMMMOOOOOMMMMIIIIIIIIIIIIIMMIIII MIM IMI M