What is more efficient?

Gary Herron gherron at digipen.edu
Mon Feb 19 00:38:48 EST 2007


Karlo Lozovina wrote:
> Let's say I have a class with few string properties and few integers, and 
> a lot of methods defined for that class. 
>
> Now if I have hundreds of thousands (or even more) of instances of that 
> class - is it more efficient to remove those methods and make them 
> separate functions, or it doesn't matter?
>
> Thanks...
>   
It is not noticeably more or less efficient in either memory or 
execution speed.

Both method and function code is compiled and stored once in either the 
class name space (for the method) or the module name space (for the 
function), and the lookup of either one is a single lookup in the 
appropriate name space.

Actually the method lookup requires one extra step: First look for the 
method in the instance (which fails) and then look for it in the class 
(which succeeds). But that extra look up is highly optimized and 
probably not noticeable. The number of methods/functions may slow things 
up, but it will affect either name space equally.

Gary Herron





More information about the Python-list mailing list