469,330 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

What is more efficient?

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

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Feb 19 '07 #1
6 1199
On Feb 19, 2:17 pm, Karlo Lozovina <_karlo_@_mosor.net_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?
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
Feb 19 '07 #2
En Mon, 19 Feb 2007 00:17:54 -0300, Karlo Lozovina <_karlo_@_mosor.net_>
escribió:
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.

--
Gabriel Genellina

Feb 19 '07 #3
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
Feb 19 '07 #4
On Feb 19, 4:38 pm, Gary Herron <gher...@digipen.eduwrote:
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.
well lucky i wont be designing/creating a new language!
>
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

Feb 19 '07 #5
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...
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__.

--
--Scott David Daniels
sc***********@acm.org
Feb 19 '07 #6
"Gabriel Genellina" <ga******@yahoo.com.arwrote in
news:ma***************************************@pyt hon.org:
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.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by William Ryan | last post: by
21 posts views Thread by Helge Jensen | last post: by
13 posts views Thread by Jason Huang | last post: by
16 posts views Thread by Matthew Zhou | last post: by
36 posts views Thread by Pat | last post: by
20 posts views Thread by athar.mirchi | last post: by
10 posts views Thread by timor.super | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.