On Thu, Mar 20, 2008 at 7:21 AM, Sharan Basappa
<[EMAIL PROTECTED]> wrote:
snip
> I am implementing two algos to solve same problem. I would like to
> measure the performance of these 2 algos
> (in terms of CPU cycles or wall clock etc.)
> I have seen some explanation in some library document in perl the
> comparison between different algos.
snip

You can use the Benchmark* module to compare two or more functions.
What follows is a script that compares various methods of rotating an
array.  You should also look into big O and little o notation.  The
first few chapters of any good algorithms book should tell you what
you need to know.

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my @a;
my %subs = (
        left_push_shift   => sub { push @a, shift @a; return 0 },
        left_slice        => sub { @a = (@a[1..$#a], $a[0]); return 0 },
        right_unshift_pop => sub { unshift @a, pop @a; return 0 },
        right_slice       => sub { @a = (pop @a, @a); return 0 },
);

print "test with ten elements\n";
for my $sub (sort keys %subs) {
        @a = 1 .. 9;
        $subs{$sub}->();
        printf "%-20s %s\n", $sub, join " ", map {"[$_]"} @a;
}

for my $n (10, 100, 1_000, 10_000) {
        @a = 1 .. $n;
        print "results for n of $n\n";
        Benchmark::cmpthese(-1, \%subs);
}

* http://perldoc.perl.org/Benchmark.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to