Optimizing Inner Loop Copy

John Machin sjmachin at lexicon.net
Thu Aug 17 21:08:00 EDT 2006


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

>
>      # as I mentioned, Rule.copy is slower than copy.copy
>      def copy(self):
>          r = Rule(self,
>                   self.rhs,
>                   self.nClasses,
>                   self.nCases)
>          return r
> 

HTH,
John




More information about the Python-list mailing list