[Tutor] Efficiency and speed
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
> 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,
Put the new design in and test it to see if it runs any faster. Ultimately
the only thing that matters (assuming its giving the correct results of
> def mcrange_gen(self, sample):
> nx2 = self.nx1
> for q in sample:
> for a in nx2:
> while a > q:
> yield a
You shouldn't need the break after the yield. The yield returns a value
and the loop iteration ends naturally.
More information about the Tutor