[Tutor] Efficiency and speed

Emile van Sebille emile at fenx.com
Fri Mar 19 20:01:38 CET 2010


On 3/19/2010 9:41 AM James Reynolds said...
<snipped and reformatted>

OK, so starting here:

def mcrange_gen(self, sample):
     lensample = len(sample)
     nx2 = self.nx1
     nx2_append = nx2.append
     nx2_sort = nx2.sort
     nx2_reverse = nx2.reverse
     nx2_index = nx2.index
     nx2_remove = nx2.remove
     for s in range(lensample):
         q = sample[s]
         nx2_append(q)
         nx2_sort()
         nx2_reverse()
         i = nx2_index(q)
         nx2_remove(q)
         yield i


First, the two lines:

     for s in range(lensample):
         q = sample[s]

variable s is never used again, so instead we'll do:

     for q in sample:

Which renders lensample as unused, so throw out

     lensample = len(sample)

We now have:


def mcrange_gen(self, sample):
     nx2 = self.nx1
     nx2_append = nx2.append
     nx2_sort = nx2.sort
     nx2_reverse = nx2.reverse
     nx2_index = nx2.index
     nx2_remove = nx2.remove
     for q in sample:
         nx2_append(q)
         nx2_sort()
         nx2_reverse()
         i = nx2_index(q)
         nx2_remove(q)
         yield i


Now, let's see what's going on for each q. You append it to self.nx1 
(through the nx2 reference), then sort nx1, then reserve nx1, then scan 
for the new position of q, note the index, remove it from nx1, and yeild 
the index.  OK, so that way you find out between which two elements of 
nx1 the current q falls, and i becomes the bin.

So, how about something like (untested):

def mcrange_gen(self, sample):
     self.nx1.sort() # now the bin boundries are in order
     for q in sample:
         ii = -1
         for binlimit in self.nx1:
             if q<binlimit:
                 break
             ii += 1
         yield ii


But, I'd probably use bisect like this:

from bisect import bisect

def mcrange_gen(self, sample):
     self.nx1.sort() # now the bin boundries are in order
     for q in sample:
         yield bisect(self.nx1,q)


HTH,

Emile



More information about the Tutor mailing list