# [Tutor] Performance of classes

Ismael Garrido ismaelgf at adinet.com.uy
Sun Jun 18 07:27:01 CEST 2006

```Hello

I'm writing a program to find an appropiate combination of resistances
to obtain a desired value of resistance. The program takes into account
how many resistances you have available and their resistance in ohms.

A short review of physics: resistors can be arranged in series or in
parallel. To obtain the equivalent resistance (that is, you could
replace those resistors with only one that had this resistance): if in
series: sum ohms; if in parallel 1/Req = 1/R1 + 1/R2.

Since this problem (as far as I can tell) requires to generate all
possible combinations (eliminating those that are impossible -ie:
requires 3 resistances of type A and you only have 2-), I'm trying to
make it run as fast as possible. So far I discovered that:
- Calling functions that return a value (ie: just "return
self.something") is slower than just grabbing the value from the class
- If you've got recursions, and you call them multiple times, you better
remember what they returned last time, otherwise it can consume a lot of
time (or to put it another way: don't go down the same road twice :)
- Psyco is nice :)
- The Python Profiler is really useful in these cases :)

Now, I was reading about decorators and noticed that some classes can
inherit from object. I thought it should make no difference in speed,
but, just because, I tried changing my class to inherit from object. Big
surprise, it *does* change things. I was wondering if somebody could
explain why it does? I find it quite weird!

Profiling normal:
4772989 function calls (3817173 primitive calls) in 54.433 CPU
seconds
Profiling all clases inherit from object:
4772989 function calls (3817173 primitive calls) in 51.151 CPU
seconds

But what's really really strange is that if one class inherits from
object but the other does not, the amount of calls varies!

Both "normal" classes:
1051803    4.609    0.000    4.609    0.000 resistPsyco.py:65(__cmp__)
78803    0.372    0.000    0.372    0.000 resistPsyco.py:85(__cmp__)

Resistencia inherits from object:
86306    0.402    0.000    0.402    0.000 resistPsyco.py:65(__cmp__)
1044300    4.879    0.000    4.879    0.000 resistPsyco.py:85(__cmp__)

Why does this happen?

I have attached the code, I hope that's fine. If you have any further
optimization suggestions I would be very glad to hear them :)

- generarPos generates all possible resistances
- generarResist generates all possible from the initial resistances
- unico makes sure that there are no duplicates (equal resistance) -
This method was posted on the ASPN Cookbook
- ResistenciaB is the basic resistance, the ones you have available.
- Resistencia is composed of two resistances
- checkPosible checks that you can actually build this resistor
- getBasicasIni tells you how many resistors of x type you have used.
- resorig are the resistors you begin with
- rescant is the amount of each basic resistors you have
- Don't put many different resistors or a big amount of resistors,
otherwise the program may take veeeryyy long to run.

Please forgive any broken English. It's been too long since I last wrote
in it.

Thanks for your time!
Ismael
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: resistPsyco.py
Url: http://mail.python.org/pipermail/tutor/attachments/20060618/84c6ab5d/attachment-0001.pot
```