Python vs Squeak

fact: 10 で測定しなおしてみました。Python は、khi さんのをお借りしました。

=> Java:       0.131 MicroSeconds per call
=> Groovy:   120.1   MicroSeconds per call
=> Ruby:      66.0   MicroSeconds per call
=> Python:    11.8   MicroSeconds per call
=> Squeak:    13.7   MicroSeconds per call
=> Squeak:     5.3   MicroSeconds per call (10 factorial)
=> VW3.1:      2.7   MicroSeconds per call
=> VW3.1:      0.4   MicroSeconds per call (10 factorial)
=> Strongtalk: 0.6   MicroSeconds per call

ついでに Strongtalk も参戦させました。

Test >> benchmark: n <Int> timesRepeat: aBlock <[]> ^<Number>
  "Test benchmark: 100000 timesRepeat: [Test fact: 10]"
  "Test benchmark: 100000 timesRepeat: [10 factorial]"
  ^ (Time millisecondsToRun: [n timesRepeat: [aBlock  value]]) / n * 1000.0

Test class >> fact: n <Int> ^<Number>
  ^ (2 to: n) inject: 1 into: [:f <Number> :i <Number> | f * i]

#inject:into: 相当では PythonSqueak はいい勝負ですね。再帰だと Ruby は速くなりますが Smalltalk ほど劇的ではありません。Python は逆に若干ですが遅くなります。

def fact2(n):
  if n == 1:
    return 1
  else:
    return n * fact2(n - 1)
def fact2(n)
  return 1 if n == 1
  n * fact2(n-1)
end