<div>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.</div>
<div><br></div><div> def mcrange_gen(self, sample):</div><div> nx2 = self.nx1</div><div> for q in sample:</div><div> for a in nx2:</div><div> while a > q:</div><div> pass</div>
<div> yield a</div><div> break</div><div><br></div><br><div class="gmail_quote">On Fri, Mar 19, 2010 at 3:15 PM, Alan Gauld <span dir="ltr"><<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">"James Reynolds" <<a href="mailto:eire1130@gmail.com" target="_blank">eire1130@gmail.com</a>> wrote<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've made a few other optimizations today that I won't be able to test until<br>
I get home, but I was wondering if any of you could give some general<br>
pointers on how to make python run a little more quickly.<br>
</blockquote>
<br></div>
Always, always, get the algorithm efficient before trying to make<br>
the code efficient.<br>
<br>
Then eliminate redundant variable assignments, extra loops,<br>
hidden loops (like in, any etc)<br>
<br>
Then use the profiler to identify the hot spots.<br>
<br>
Then fine tune the hot spots.<br>
This is where you can start to worry about the speedups of<br>
using local variables etc.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
There are two major pieces of code that seem slow, so I'll share the first<br>
with you. This section takes up about 1/3 of the time used when running all<br>
trials, where trials is 10K or larger.<br>
</blockquote>
<br></div>
How are you measuring? Is it via the profiler? Is it by inserying print<br>
time statements? Is is subjectively timing it by hand?<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The second section, the one that is taking up most of the time, does the<br>
math.<br>
</blockquote>
<br></div>
Thats probably what you would expect if the math is complex.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The list nx1 is about 220 floating point numbers long.<br>
</blockquote>
<br></div>
So not very big at all...<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample<br>
values based on nx1<br>
</blockquote></blockquote>
<br></div>
The use of self suggests there is an object, or at least a class definition involved?<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
for s in self.mcrange_gen(sample):<br>
countlist.append(s-1) # This appends the bin number (the number<br>
</blockquote>
<br>
</div><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
def mcrange_gen(self, sample):#, lensample):<br>
lensample = len(sample) # this section is just for speed. All of these<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
are renames from the globals to bring calc times down at the expense of<br>
memory. I haven't tested these yet.<br>
</blockquote></blockquote>
<br></div>
This is premature optimisation at this stage. Its cluttering up the code<br>
for relatively little benefit.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
for s in range(lensample):<br>
q = sample[s] #takes the next randomly generated number from the<br>
</blockquote>
<br></div>
for q in sample<br>
<br>
would be more pythonic<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
nx2_append(q) # and appends it to nx list.<br>
nx2_sort() # and sorts it in place<br>
nx2_reverse() # reverses the list, because this was the original<br>
</blockquote>
<br></div>
So you sort and reverse the entire list every time round the for loop?<br>
Might it be more efficient to keep the list in the right order to start with?<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
i = nx2_index(q) #get the index of that element<br>
nx2_remove(q) # and remove the element.<br>
</blockquote>
<br></div>
Now you find the thing you inserted and remove it. Wow.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
yield i # send the position of that element back to the main<br>
</blockquote>
<br></div>
So you really just want to find out where you would like to insert it<br>
in an already sorted/reversed list?<br>
<br>
Back to step one - can you improve the algorithm?<br><font color="#888888">
<br>
-- <br>
Alan Gauld<br>
Author of the Learn to Program web site<br>
<a href="http://www.alan-g.me.uk/" target="_blank">http://www.alan-g.me.uk/</a> <br></font><div><div></div><div class="h5">
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br>