Ineffectual has asked for the wisdom of the Perl Monks concerning the following question:

I have two separate scripts that I'd like to compare against each other. Each script takes different variables and runs in a different directory. Each one is somewhat OO and has a library that it calls that contains the subroutines that it goes through.

My very limited understanding of Benchmark from reading the docs led to me using:
$file = 'scriptname var var'; cmpthese(-5, {a=>$file, b=>$file2})

However, Benchmark errors when it tries to do the variables asociated with each script. In addition, one of the scripts has a dash (-) in the filename and there seems to be some issue with that as well.

I'd appreciate any help pointing me in the right direction.
Thanks.
'Fect

Replies are listed 'Best First'.
Re: First time with Benchmark
by cchampion (Curate) on Mar 27, 2003 at 18:18 UTC

    cmpthese requires an anonymous hash, whose values are subs to execute. This should be better.

    #!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my $file1 = 'first.pl'; my $file2 = 'second.pl'; cmpthese (-5, { 'first' => sub { chdir "/home/firstdir/"; do $file1}, 'second'=> sub { chdir "/home/seconddir/"; do $file2} });
Re: First time with Benchmark
by BrowserUk (Patriarch) on Mar 27, 2003 at 18:23 UTC

    Benchmark is generally used to time/compare either subroutines or strings of code that can be eval'd rather than whole external scripts.

    That said, you might get away with using "`scriptname var var`" or q[system "scriptname var var"]. Ie. Invoke the scripts using backtickes or system.

    This would mean that you would be timing not just the run-time of the scripts, but also the load and compilation time plus the time taken to fork a process, find and load the shell (could be avoided) and the perl executable, which may or may not be useful for your purpose. There are also other possible caveats of doing it this way.

    If you want to time a complete script from start to finish like this, many OS's have a simple shell command for doing so. Native CMD doesn't, but depending on how accurate you need the timing to be, you can get close by setting the prompt to include the current time ($T) and then do the math yourself. There are also several freeware tools around that can do this.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: First time with Benchmark
by Limbic~Region (Chancellor) on Mar 27, 2003 at 22:04 UTC
    Ineffectual,
    In addition to using the information already presented, you should do a Super Search on benchmarking. Since the title of your node was "first time", I expect that you might run into some of the same problems I did my first time around. The two biggest things I will point out are:

  • Run your tests on an inactive system (single user mode with cron disabled if on *nix if possible)
  • If you are using external data, vary it. Comparing algorithms to split a strings will tend to give biased information if the input is not varied.

    You might also want to check out http://www.perlfaq.com and http://www.perldoc.com for frequently asked questions on the best practices of profiling your code.

    Cheers - L~R