What is more efficient?

K

Karlo Lozovina

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...
 
P

placid

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?

What do you mean by efficient. Memory efficient? I would assume that
making them separate functions would use less memory but would you
sacrifice code readability for that (small?) of a memory saving?

Good coding practice suggests that the methods should be bound to
classes.
Thanks...

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web:http://www.mosor.net|| ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|



Cheers
 
G

Gabriel Genellina

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?

I'm not sure what you mean, but normal methods are attached to the class,
not to its instances. It doesn't matter whether you have 0 or a million
instances, methods do not occupy more memory.
 
G

Gary Herron

Karlo said:
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
 
S

Scott David Daniels

Karlo said:
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...
You'd do best to define the instance variable names in __slots__,
and be sure to inherit from object. The methods don't matter (they
all hang off the class anyway). The __slots__ will save you one
reference per object for the __dict__.
 
K

Karlo Lozovina

It doesn't matter whether you have 0 or a million instances,
methods do not occupy more memory.

That's what I was looking for!
Thanks, to you and all the others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,284
Latest member
RedaBruno6

Latest Threads

Top