where to create the object

Niels Diepeveen niels at endea.demon.nl
Fri Mar 3 18:05:40 CET 2000


Oivvio Polite schreef:

> the method  result of class foo generates a the result object bar.
> the bar object could either created in the result method and returned to
> the calling program.
> or it could be created in the calling program, supplied as an argument
> to result that results just fills up with the right values.
> 
> my gut feeling is that the first choice is a better design but that the
> second choice is less costly in terms of both memory and time.

I mostly agree with your gut, but I don't see any difference in the
memory usage. I would think that instances are probably destroyed about
as fast as they are allocated, unless you need to keep them, in which
case you would still have to allocate a new one for every call.

> 
> what are your any rules of thumb?

Before breaking a perfectly good design to make it faster, ask yourself:
Is this piece of code likely to be a time-critical part of some
application?
How much time can I actually save? (run some tests)
Would it be hard to add some optimization later on?

In most cases, the answer to at least one of these questions is 'no' or
'not much'.

For the answer to the last question, in this case: the optional argument
solution offered by Emile can easily be retrofitted.

-- 
Niels Diepeveen
Endea automatisering




More information about the Python-list mailing list