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} } ); -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!