$|=1;
warn "I am ", __FILE__, "\n";
my $mainfile = $0;


sub DB::DB {
  my $et = time;
  my ($package, $filename, $line) = caller;
  $filename{$filename}=1;
  $st = time, return unless $filename eq $0;
  $count[$line]++;
  $tc++;
  $time[$line] += $et - $st;
  $tt += $et - $st;
  $st = time;
}

BEGIN { open PROF, ">", "prof.out" or die $! }

use Time::HiRes 'time';

END {
  print PROF join(" ", keys %filename), "\n";
  my @r = sort {$time[$b] <=> $time[$a]} (0.. $#count);
  my @ch;
  for (0..9) {
    $ch[$r[$_]] = '*';
  }
  for (10..24) {
    $ch[$r[$_]] = '+';
  }
  for (26..99) {
    $ch[$r[$_]] = '-';
  }
  for (100..249) {
    $ch[$r[$_]] = '.';
  }
  for (1 .. $#count) {
    my ($c, $t) = ($count[$_], $time[$_]);
    local *L = \@{"::_<$mainfile"};
    my $L = $L[$_];
    chomp $L;
    $L = substr($L, 0, 54);
    if ($c) {
      printf PROF "%4d%s%6d %5.2f %5.2f %-54s\n", $_, 
        $ch[$_] || ' ',
        $c, 100*$c/$tc, 100*$t/$tt,, $L;
    } else {
      printf PROF "%4d                    %-54s\n", $_, $L;
    }
  }
}

"I like pie";
__END__

Since DB::DB is called every time a statement is executed, I can
accumulate a count of the number of times each statement is executed,
and an approximate amount of time spent in each statement.  An END
block prints out a report:

 108                      sub FETCH {
 109   1000 0.210 0.00021   my ($self, $n) = @_;
 110   1000 0.047 0.00005   my $rec;
 111                      
 112                        # check the defer buffer
 113   1000 0.028 0.00003   if ($self->_is_deferring && exists $self->{deferred
 114                          $rec = $self->{deferred}{$n};
 115                        } else {
 116   1000 0.072 0.00007     $rec = $self->_fetch($n);
 117                        }
 118                      
 119   1000 0.060 0.00006   $self->_chomp1($rec);
 120                      }
