[Tutor] Efficiency and speed
Dave Angel
davea at ieee.org
Sat Mar 20 15:03:55 CET 2010
(Please don't top-post. It ruins the context for anyone else trying to
follow it. Post your remarks at the end, or immediately after whatever
you're commenting on.)
James Reynolds 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.
>
> def mcrange_gen(self, sample):
> nx2 = self.nx1
> for q in sample:
> for a in nx2:
> while a > q:
> pass
> yield a
> break
>
>
> On Fri, Mar 19, 2010 at 3:15 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
>
>
While loops and for loops are not slow, it's the algorithm that you're
using that's slow. If a while loop is the best way to do the best
algorithm, then it's fast. Anyway, in addition to for and while, other
"slow" approaches are find() and "in".
But slowest of all is a loop that never terminates, like the while loop
in this example. And once you fix that, the break is another problem,
since it means you'll never do more than one value from sample.
In your original example, you seemed to be calling a bunch of methods
that are each probably a single python statement. I didn't respond to
those, because I couldn't really figure what you were trying to do with
them. But now I'll comment in general terms.
Perhaps you should do something like:
zip together the original list with a range list, so you now have a list
of tuples. Then sort that new list. Now loop through that sorted list
of tuples, and loop up your bucket for each item. That should be fast
because they're in order, and you have the index to the original
value, so you can store the bucket number somewhere useful.
HTH,
DaveA
More information about the Tutor
mailing list