James Edward Gray II wrote:

/ ...

> #!/usr/bin/env ruby -w
> 
> require "benchmark"
> 
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
>    results.report("Exponent:") { TESTS.times { |n| n ** 2 } }
>    results.report("Multiply:") { TESTS.times { |n| n * n } }
> end
> # >> Rehearsal ---------------------------------------------
> # >> Exponent:   1.370000   0.000000   1.370000 (  1.372358)
> # >> Multiply:   1.730000   0.010000   1.740000 (  1.747647)
> # >> ------------------------------------ total: 3.110000sec
> # >>
> # >>                 user     system      total        real
> # >> Exponent:   1.440000   0.000000   1.440000 (  1.452441)
> # >> Multiply:   1.760000   0.010000   1.770000 (  1.775988)

One can't argue with results, but this is some kind of language-specific
anomaly if one considers the operations (presumably) taking place behind
the scenes. For arbitrary numbers n,p and the operation n^p, 

n^p = e^(log(n)*p)

where "e" = base of natural logarithms, "log" = base e logarithm.

So it seems there's at least one multiply no matter what one does. So, for
n^2, it's hard to see how n * n could be slower, for either integers or
floats.

But ... in this case, it is.

-- 
Paul Lutus
http://www.arachnoid.com