lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mark Hamburg wrote:
> Note that this system incurs the overhead of allocating a closure on every
> method call. If one is willing to pay that overhead, it's probably worth
> looking at what Python does on method dispatch since as I recall it has a
> fairly flexible implementation but it too pays the closure creation price.

Python creates the method bindings (closures) dynamically on every access.
So unless you cache them yourself in your code, they are just food for the
garbage collector after each call is done. NB: Jython may be different.

The attached test program shows that the overhead of creating and discarding
the binding comes close to the overhead of the pure method call itself.
Depending on the version and compiler options I get numbers from 150% - 200%.

Not a good example to follow.



from time import clock

class Foo(object):
  def method(self): pass

def test(obj):
  n = 1000000

  start = clock()
  for i in xrange(n): pass
  base = clock() - start

  start = clock()
  for i in xrange(n): obj.method()
  delta1 = clock() - start - base

  m = obj.method
  start = clock()
  for i in xrange(n): m()
  delta2 = clock() - start - base

  print 100.0 * delta1 / delta2