Optimizing Inner Loop Copy
Michael Spencer
mahs at telcopartners.com
Thu Aug 17 22:24:34 EDT 2006
Mark E. Fenner wrote:
> Michael Spencer wrote:
>
>> Mark E. Fenner wrote:
>>
>>> and the copy is taking the majority (42%) of my execution time.
>>> So, I'd like to speed up my copy. I had an explicit copy method that did
>>> what was needed and returned a new object, but this was quite a bit
>>> slower than using the standard lib copy.copy().
>>>
>> How are you measuring? It seems to me that your Rule.copy method is a lot
>> faster than copy.copy:
>>
>> >>> r= Rule(range(100))
>> >>> shell.timefunc(r.copy)
>> 'copy(...) 36458 iterations, 13.71usec per call'
>> >>> from copy import copy
>> >>> shell.timefunc(copy, r)
>> 'copy(...) 4498 iterations, 111.17usec per call'
>
> <snip>
>
>> Michael
>
> Michael,
>
> Thank you. I misinterpreted something somewhere ... the program is indeed
> faster using Rule.copy. I need to look over the rest of my profiling data,
> to see if I screwed up elsewhere as well.
>
> So, how to optimize Rule.copy()?
>
> Regards,
> Mark
You're not doing much in the loop, so there isn't much to change:
def copy2(self):
new_rule = list.__new__(Rule)
new_rule[:] = self
new_rule.__dict__.update(self.__dict__)
return new_rule
measures slightly (5-10%) faster for me - but hardly worth the obscurity
You could bind allNew.append outside the loop, and perhaps merge the Rule.modify
method into Rule.copy to save a call.
Given that a no-op method call, takes 2.7 usec on my machine, and you have 3 or
4 method calls per copy, I suspect you're near the limit of a pure-Python
solution with this object structure.
>>> shell.timefunc(r.nop)
'nop(...) 185488 iterations, 2.70usec per call'
Assuming you're going to be doing some material work with these rules, I wonder
if it's actually worth your the effort to make the copying go faster.
(Sorry no better ideas)
Michael
More information about the Python-list
mailing list