[Tutor] Efficiency and speed

Alan Gauld alan.gauld at btinternet.com
Sat Mar 20 02:07:19 CET 2010


"James Reynolds" <eire1130 at gmail.com> wrote

> Here's another idea I had. I thought this would be slower than then the
> previous algorithm because it has another for loop and another while 
> loop. I
> read that the overhead of such loops is high, so I have been trying to 
> avoid
> using them where possible.

Thats often true but nested loops are sometimes unavoidable.
Its unnecessary nests that are really bad!

But the other important rule when tuning performamce is: don't guess, 
measure.
Put the new design in and test it to see if it runs any faster. Ultimately 
thats
the only thing that matters (assuming its giving the correct results of 
course!)

>    def mcrange_gen(self, sample):
>        nx2 = self.nx1
>        for q in sample:
>            for a in nx2:
>                while a > q:
>                     pass
>            yield a
>            break

You shouldn't need the break after the yield. The yield returns a value
and the loop iteration ends naturally.

Alan G. 




More information about the Tutor mailing list