Answer to number 3: run (under windows)
perldoc Benchmark
to read the POD for the module. If you have a somewhat old and/or broken install (like me), you can always fall back to
vi \perl\lib\Benchmark.pm
:)
What you've written is a proper way of using benchmark. Ideally, you would insert several objects into your code to get the times you wanted, but you can do it in a clever and inobtrusive manner:
BEGIN {
use Benchmark;
my ($oldb, $newb); # inside BEGIN for closure
sub mybench {
return unless ($opt_b);
$newb = new Benchmark;
if ($oldb) { # i.e. don't run first time through
print timediff($oldb, $newb);
}
$oldb = $newb;
}
}
Getopt::Std;
getopts('b');
mybench();
# some code here
mybench();
# some more code here
mybench();
and then it would be trivial to remove the benchmarking from your production code, either through a command line switch, as above:
mycode.pl -b
or some line in your makefile like:
perl -lne 'print unless /^\s*mybench();/' myscript.pl > production.pl
ya know.