Optimizing Inner Loop Copy
Mark E. Fenner
mfenner at gmail.com
Thu Aug 17 22:13:09 EDT 2006
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
More information about the Python-list
mailing list