#!/usr/bin/perl -w
use strict;
use Benchmark;

my $source = join("\n", 'a'..'zz');

print join(',', b_tr(), b_ss(), b_in(), b_gr(), b_ma(), b_su(), b_ev()), "\n";

#-----------
Benchmark::cmpthese(-1, {
  b_tr=>\&b_tr, b_ss=>\&b_ss, b_in=>\&b_in,
  b_gr=>\&b_gr, b_ma=>\&b_ma, b_su=>\&b_su,
  b_ev=>\&b_ev,
});

#-----------
sub b_tr{
  my $cnt = $source =~ tr/\n//;
  if(substr($source, -1) ne "\n"){ $cnt++; }
  return $cnt;
}

#-----------
sub b_ss{
  my $cnt = $source =~ s/\n/\n/g;
  if(substr($source, -1) ne "\n"){ $cnt++; }
  return $cnt;
}

#-----------
sub b_in{
  my($i,$cnt)=(0,0);
  while(($i=index($source, "\n", $i+1)) >= 0){ $cnt++; }
  if(substr($source, -1) ne "\n"){ $cnt++; }
  return $cnt;
}

#-----------
sub b_gr{ return scalar grep(/./, split("\n", $source)); }
sub b_ma{ return scalar map { 1 } split("\n", $source);  }
sub b_su{ return scalar sub{  my @a=split("\n", $source); }->(); }
sub b_ev{ return scalar eval{ my @a=split("\n", $source); };     }