--- NickC <[EMAIL PROTECTED]> wrote:
> I'd be very interested to hear your opinion on the 'namespace' module,
> which looks at addressing some of these issues (the Record object, in
> particular).  The URL is http://namespace.python-hosting.com, and any
> comments should be directed to the [EMAIL PROTECTED]
> discussion list.

Hi Nick,

The namespace module looks interesting, thanks for the pointer! (I saw your
other message but didn't have a chance to reply immediately.)

I tried out the namespace.Record class. The resulting user code looks nice, but
I have two concerns:

- It requires a different coding style; until it is well established it will
surprise people.

- The runtime penalty is severe.

Attached is a simple adopt_timings.py script. If I run it with Python 2.4.1
under RH WS3, 2.8GHz Xeon, I get:

    overhead: 0.01
    plain_grouping: 0.27
    update_grouping: 0.44
    plain_adopt_grouping: 0.68
    record_grouping: 10.85

I.e. record_grouping (using namespace.Record) is about 40 times slower than the
manual self.x=x etc. implementation.

My conclusion is that namespace.Record may have merits for specific purposes,
but is impractical as a general-purpose utility like I have in mind.

Note that the attached code includes a new, highly simplified "plain_adopt()"
function, based on the information I got through other messages in this thread.
Thanks to everybody for suggestions!

Cheers,
        Ralf


                
____________________________________________________
Sell on Yahoo! Auctions – no fees. Bid on great items.  
http://auctions.yahoo.com/
import sys, os

class plain_grouping:
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

class update_grouping:
  def __init__(self, x, y, z):
    self.__dict__.update(locals())
    del self.self

def plain_adopt():
  frame = sys._getframe(1)
  init_locals = frame.f_locals
  self = init_locals[frame.f_code.co_varnames[0]]
  self.__dict__.update(init_locals)
  del self.self

class plain_adopt_grouping:
  def __init__(self, x, y, z):
    plain_adopt()

try:
  from namespace import Record
except ImportError:
  Record = None
else:
  class record_grouping(Record):
    x = None
    y = None
    z = None

class timer:
  def __init__(self):
    self.t0 = os.times()
  def get(self):
    tn = os.times()
    return (tn[0]+tn[1]-self.t0[0]-self.t0[1])

def time_overhead(n_repeats):
  t = timer()
  for i in xrange(n_repeats):
    pass
  return t.get()

def time(method, n_repeats):
  g = method(x=1,y=2,z=3)
  assert g.x == 1
  assert g.y == 2
  assert g.z == 3
  t = timer()
  for i in xrange(n_repeats):
    method(x=1,y=2,z=3)
  return t.get()

def time_all(n_repeats=100000):
  print "overhead: %.2f" % time_overhead(n_repeats)
  print "plain_grouping: %.2f" % time(plain_grouping, n_repeats)
  print "update_grouping: %.2f" % time(update_grouping, n_repeats)
  print "plain_adopt_grouping: %.2f" % time(plain_adopt_grouping, n_repeats)
  if (Record is not None):
    print "record_grouping: %.2f" % time(record_grouping, n_repeats)

if (__name__ == "__main__"):
  time_all()
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to