By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,972 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,972 IT Pros & Developers. It's quick & easy.

What is more efficient?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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.