in reply to Re: Convert arithmetic expression from Excel to Perl syntax
in thread Convert arithmetic expression from Excel to Perl syntax

You can also di $temp**2 or $vol**2. Exactly the same, but faster.
  • Comment on Re^2: Convert arithmetic expression from Excel to Perl syntax

Replies are listed 'Best First'.
Re^3: Convert arithmetic expression from Excel to Perl syntax
by ikegami (Patriarch) on Nov 04, 2004 at 18:14 UTC
    You're right that ** works too, but you're wrong about it being faster (at least on my system).
    # srand(0) is used to make sure both # subs are using the same input set. sub m1 { unless ($i1) { srand(0); $i1=1; } $n = rand; $p = $n ** 2; } sub m2 { unless ($i2) { srand(0); $i2=1; } $n = rand; $p = $n * $n; } use Benchmark; Benchmark::cmpthese(0, { '$n**2' => \&m1, '$n*$n' => \&m2, }); __END__ Rate $n**2 $n*$n $n**2 722292/s -- -29% $n*$n 1013884/s 40% --
      when i said it was faster i meant that 'writing it' is faster