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