Home > Archive > PERL Beginners > March 2008 > measuring performance
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
measuring performance
|
|
| Sharan Basappa 2008-03-20, 8:02 am |
| * Haven't done this before, so need help *
Hi,
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.
Regards
| |
| Jenda Krynicky 2008-03-20, 8:02 am |
| Date sent: Thu, 20 Mar 2008 16:51:52 +0530
From: "Sharan Basappa" <sharan.basappa@gmail.com>
To: beginners@perl.org
Subject: measuring performance
> * Haven't done this before, so need help *
>
> Hi,
>
> 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.
perldoc Benchmark
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Chas. Owens 2008-03-20, 8:02 am |
| On Thu, Mar 20, 2008 at 7:21 AM, Sharan Basappa
<sharan.basappa@gmail.com> 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.
| |
| Sharan Basappa 2008-03-20, 7:02 pm |
| Thanks, this is really helpful. In addition, is there a way to print
the cpu cycles taken from *ux command prompt?
I have worked with tools that, at the end of their job, print out the
cpu cycles it took for them.
I would assume that they use some command from *ux to do this.
Regards
On Thu, Mar 20, 2008 at 6:21 PM, Chas. Owens <chas.owens@gmail.com> wrote:
> On Thu, Mar 20, 2008 at 7:21 AM, Sharan Basappa
> <sharan.basappa@gmail.com> wrote:
> snip
>
> 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.
>
| |
| Chas. Owens 2008-03-20, 7:02 pm |
| On Thu, Mar 20, 2008 at 9:15 AM, Sharan Basappa
<sharan.basappa@gmail.com> wrote:
> Thanks, this is really helpful. In addition, is there a way to print
> the cpu cycles taken from *ux command prompt?
> I have worked with tools that, at the end of their job, print out the
> cpu cycles it took for them.
> I would assume that they use some command from *ux to do this.
snip
The time command in UNIX will give you the amount of time the program
took to run, the amount of cpu time the program took to run, and the
amount of time spent on system overhead:
time ./q.pl
real 0m0.013s
user 0m0.007s
sys 0m0.006s
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
| |
| John W. Krahn 2008-03-20, 7:02 pm |
| Chas. Owens wrote:
> On Thu, Mar 20, 2008 at 9:15 AM, Sharan Basappa
> <sharan.basappa@gmail.com> wrote:
> snip
>
> The time command in UNIX will give you the amount of time the program
> took to run, the amount of cpu time the program took to run, and the
> amount of time spent on system overhead:
>
> time ./q.pl
>
> real 0m0.013s
> user 0m0.007s
> sys 0m0.006s
Or you could use the times() function built in to Perl.
perldoc -f times
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Sharan Basappa 2008-03-20, 7:02 pm |
| ok, here is the issue I am having measuring the performance of a unix
process I am trying to execute
within perl. Within per because I want to use benchmark module to see
how long it takes to execute
this process.
Here is the code snippet:
#!/usr/bin/perl
use Benchmark;
# declare array
my @data;
# start timer
$start = new Benchmark;
system ("vcs -sverilog Bins.sv -ntb_opts dtm -R");
$end = new Benchmark;
# calculate difference
$diff = timediff($end, $start);
# report
print "Time taken was ", timestr($diff, 'all'), " seconds";
The issue is that perl reports: Time taken was 1 wallclock secs
I think this is because perl is launching the process (vcs) and exits
(or is it?) and
hence there is really no time consumed between start and end. Can someone
comment?
Regards
On Thu, Mar 20, 2008 at 6:58 PM, John W. Krahn <krahnj@telus.net> wrote:
> Chas. Owens wrote:
>
> Or you could use the times() function built in to Perl.
>
> perldoc -f times
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| Jim Gibson 2008-03-20, 7:02 pm |
| In article
< 8a31202a0803200639m5b7d14fftfd58f4126d50
2a09@mail.gmail.com>, Sharan
Basappa <sharan.basappa@gmail.com> wrote:
> ok, here is the issue I am having measuring the performance of a unix
> process I am trying to execute
> within perl. Within per because I want to use benchmark module to see
> how long it takes to execute
> this process.
>
> Here is the code snippet:
>
> #!/usr/bin/perl
>
> use Benchmark;
> # declare array
> my @data;
> # start timer
> $start = new Benchmark;
> system ("vcs -sverilog Bins.sv -ntb_opts dtm -R");
> $end = new Benchmark;
> # calculate difference
> $diff = timediff($end, $start);
> # report
> print "Time taken was ", timestr($diff, 'all'), " seconds";
>
> The issue is that perl reports: Time taken was 1 wallclock secs
>
> I think this is because perl is launching the process (vcs) and exits
> (or is it?) and
> hence there is really no time consumed between start and end. Can someone
> comment?
The resolution of the basic Benchmark module measurements is 1 second.
It is usually used to time Perl code that may be repeated many times
and the average time for each iteration calculated by division.
Try installing the Time::HiRes module and adding the ':hireswallclock'
argument:
#!/usr/local/bin/perl
use strict;
use warnings;
use Benchmark qw(:hireswallclock);
my $t0 = new Benchmark;
system("sleep 1");
my $t1 = new Benchmark;
print "Time taken was ", timestr(timediff($t1,$t0)), "\n";
Output:
Time taken was 1.00239 wallclock secs ( 0.00 usr + 0.00 sys = 0.00
CPU)
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
|
|
|
|
|