Optimisation problem - in C?

Sebastian Dauss sebastian.dauss at web.de
Wed Feb 21 09:11:24 EST 2001

> It's too bad that lists don't have a .get() method (so x.get(3) would be
> equal to x[3]), because resultvector=map(sourcelist.get, elementlist)
> would be fastest (all done in C).

Oh, in fact there is a list get()-method. It is hidden within the
'operator'-module ;). The only disadvantage is that it expects the sequence
to fetch the item from in its first argument, so you have to make a list of
references to the sourcelist in order to apply the map()-function. If memory
consumption is not an issue, you can try to do the most work at C-speed by

   import operator
   def makevector_operator(sourcelist,elementlist):
       return map(operator.getitem, [sourcelist]*len(elementlist),

Let's summarize and profile the different approaches (300000 items in
sourcelist, 70000 in elementlist with random indices):

   Runtime UsrTim SysTim Count  TimerName
  0.517912   0.50   0.01     1: makevector
  0.361448   0.33   0.01     1: makevector_listc
  0.222591   0.22   0.00     1: makevector_noappend
  0.221596   0.22   0.00     1: makevector_lambda
  0.183666   0.19   0.00     1: makevector_operator

def makevector(sourcelist,elementlist):
    resultvector = []
    for element in elementlist:
    return resultvector

def makevector_noappend(sourcelist, elementlist):
    resultvector = elementlist[:]
    for i in range(len(elementlist)):
       resultvector[i] = sourcelist[elementlist[i]]
    return resultvector

def makevector_lambda(sourcelist,elementlist):
    return map(lambda i, l=sourcelist: l[i], elementlist)

def makevector_listc(sourcelist,elementlist):
    return [sourcelist[element] for element in elementlist]

def makevector_operator(sourcelist,elementlist):
    return map(operator.getitem, [sourcelist]*len(elementlist), elementlist)

Of course, now I'm very interested in the performance of a C-implementation
Maybe it would help to have something like a "constant sequence" in C to
avoid generating the reference-list. Then, you could write:

listreferences = xReferenceList(sourcelist, len(elementlist))
return map(operator.getitem, listreferences, elementlist)


More information about the Python-list mailing list