On Wed, Nov 16, 2005 at 04:57:30PM -0500 Jay Savage wrote:
> On 11/16/05, Gerard Robin <[EMAIL PROTECTED]> wrote:
> > On Wed, Nov 16, 2005 at 05:17:29AM -0800 John W. Krahn wrote:
> >
> > > The first one should be faster.  Of course you could use the Benchmark 
> > > module
> > > to find out for sure.
> >
> > Thanks for your help.
> >
> > I tried Benchmark but the results seem not reliable:
> >
> > cmpthese( -5, { a => sub{$x*$x}, b => sub{$x**2} } );
> >
> > outputs:
> >        Rate    b    a
> > b 3858660/s   -- -19%
> > a 4757428/s  23%   --
> >
> >
> > cmpthese( -5, { a => sub{$x**2}, b => sub{$x*$x} } );
> >
> > outputs:
> >        Rate    b    a
> > b 4401295/s   -- -14%
> > a 5141629/s  17%   --
> >
> > perhaps I missed something ?
> >
> 
> can we see the rest of your code? Where are you declaring $x? Try
> using two variables with the same starting value. Or reset the value
> on each iteration. Otherwise the second routine to execute is
> multiplying much, much larger numbers because the value is squared on
> each iteration:
> 
>     my $x = 2;
>     my $y = 2;
>     cmpthese( -5, { a => sub{$x**2}, b => sub{$y*$y} } );
> 
> or
> 
>     my $x = 2;
>     cmpthese( -5, { a => sub{$x**2; $x = 2}, b => sub{$x*$x; $x = 2} } );

 
I used the example in "perldoc Benchmark":

--------------------------------------------------------
#!/usr/bin/perl
# bench1.pl

use warnings;
use Benchmark qw( cmpthese );

$x = 3;

cmpthese( -5, { a => sub{$x*$x}, b => sub{$x**2} } );

-------------------------------------------------------

and bench2.pl with: cmpthese( -5, { a => sub{$x**2}, b => sub{$x*$x} } );

instead of cmpthese( -5, { a => sub{$x*$x}, b => sub{$x**2} } );



-- 
Gérard


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


Reply via email to