Arnaud Bergeron wrote: > I did my own benchmarking and my results are contrary to what was said > before. / ... snip demonstration in which n * n is 3.2 x faster than n ** 2 > It shows that n * n is faster. > > However this may be an architecture difference. (for reference I'm on > a mac, a G3) Interesting. It seems to be implementation-related after all. On the other hand, if this and the prior result are averaged, there seems little overall difference, so for a typical programmer with an arbitrary platform, it might be a toss-up. Here are my tests (Linux on an Intel Xeon, clock 3 GHz): ----------------------------------------- #!/usr/bin/ruby require 'benchmark' T=1000000 n = 413 Benchmark.bmbm(10) do |x| x.report("n * n") { T.times { n * n } } x.report("n ** 2") { T.times { n ** 2 } } end ----------------------------------------- Result: Rehearsal --------------------------------------------- n * n 1.033333 0.000000 1.033333 ( 0.615591) n ** 2 2.083333 0.000000 2.083333 ( 1.262649) ------------------------------------ total: 3.116667sec user system total real n * n 1.133333 0.000000 1.133333 ( 0.686552) n ** 2 2.283333 0.000000 2.283333 ( 1.377575) In this example (n = 413), n * n is about twice as fast as n ** 2. But ... if I choose a larger value for n (41398765432), so that the computation is carried out using Bignum: Rehearsal --------------------------------------------- n * n 1.566667 0.016667 1.583333 ( 0.953782) n ** 2 1.633333 0.000000 1.633333 ( 0.992337) ------------------------------------ total: 3.216667sec user system total real n * n 1.583333 0.000000 1.583333 ( 0.954614) n ** 2 1.650000 0.016667 1.666667 ( 1.007131) A much smaller advantage for n * n when n is a Bignum. Then, using a much bigger number (413987654321987654321): Rehearsal --------------------------------------------- n * n 1.816667 0.000000 1.816667 ( 1.095099) n ** 2 1.866667 0.000000 1.866667 ( 1.128506) ------------------------------------ total: 3.683333sec user system total real n * n 1.850000 0.016667 1.866667 ( 1.139751) n ** 2 1.850000 0.000000 1.850000 ( 1.114387) In this case, n * n loses its advantage by a slight margin. Overall, a toss-up. -- Paul Lutus http://www.arachnoid.com