danielx wrote: > > Mark E. Fenner wrote: >> Mark E. Fenner wrote: >> >> > John Machin wrote: >> > >> >> >> >> Mark E. Fenner wrote: >> >> >> >>> Here's my class of the objects being copied: >> >> >> >> Here's a couple of things that might help speed up your __init__ >> >> method, and hence your copy method: >> >> >> >>> >> >>> class Rule(list): >> >>> def __init__(self, lhs=None, rhs=None, nClasses=0, nCases=0): >> >> >> >> def __init__(self, lhs=None, rhs=(), nClasses=0, nCases=0): >> >> >> >>> self.nClasses = nClasses >> >>> self.nCases = nCases >> >>> >> >>> if lhs is not None: >> >>> self.extend(lhs) >> >> what does the extend method do? If it is small, perhaps inline a copy >> >> of its code here. >> >>> >> >>> if rhs is None: >> >>> self.rhs=tuple() >> >>> else: >> >>> self.rhs=rhs >> >> >> >> Replace the above 4 lines by: >> >> self.rhs = rhs >> >> >> >> HTH, >> >> John >> > >> > John, >> > >> > Thanks. I thought of those at the same you did! I also incorporated >> > one other use of the default=() + no conditional: >> > >> > class Rule(list): >> > def __init__(self, lhs=(), rhs=(), nClasses=0, nCases=0): >> > self.nClasses = nClasses >> > self.nCases = nCases >> > self.extend(lhs) # note, self is a list so this is list.extend >> > self.rhs=rhs >> > >> > def copy(self): >> > return Rule(self, >> > self.rhs, >> > self.nClasses, >> > self.nCases) >> >> >> Actually, I also removed the "passthrough" that copy was doing and just >> called the constructor directly. So, at the top level code, we have: >> >> allNew = [] >> for params in cases: >> # newobj = initialObject.copy() >> newObj = Rule(initialObject, initialObject.rhs, >> initialObject.nClasses, >> initialObject.nCases) >> newObj.modify(params) >> allNew.append(newObj) >> return allNew >> >> Regards, >> Mark > > I'm not sure how much this will help, but another thing you can do is > put this line before the "for": > > append = allNew.append > > Then, replace the last line in the loop with > > append(newObj) > > Check out this doc for more info on optimizing Python, and the section > which talks about eliminating dots: > > http://wiki.python.org/moin/PythonSpeed/PerformanceTips > http://wiki.python.org/moin/PythonSpeed/PerformanceTips#head-aa6c07c46a630a2fa10bd6502510e532806f1f62
I've read these, but they do yield additional nuggets upon rereading (does that make them generators?). In reality, my loop has several lines of code instead of a newObj.modify(args) method. I localised the obvious functions and attributes that I was using. In reality, the Rule.copy/Rule.__init__ is swamping everything else ... humm, unless ... I'll get back to you. Regards, Mark -- http://mail.python.org/mailman/listinfo/python-list