can this be made faster?

Johannes Loehnert a.u.r.e.l.i.a.n at
Tue Oct 10 03:03:27 CDT 2006


> > This seems like a rather common operation - I know I've needed it on
> > at least two occasions - is it worth creating some sort of C
> > implementation? What is the appropriate generalization?
> Some sort of indirect addressing infrastructure. But it looks like this
> could be tricky to make safe, it would need to do bounds checking at the
> least and would probably work best with a contiguous array as the target. I
> could see some sort of low-level function called argassign(target, indirect
> index, source) that could be used to build more complicated things in
> python.

This looks somehow like the behaviour of builtin map. One could do map(fn, 
index) with appropriate fn. But iirc this is not faster than a for loop if fn 
is not a builtin function.

An infrastructure like you imagine might use a similar syntax (with underlying 
C funcs). The main point is, how to tell it which operation to perform (add, 
multiply, average, whatever). Implementing a bunch of functions 
add_argassign, ... whatever_argassign contradicts my understanding of 
"generalized". ;)

Maybe it would be simpler to just have functions which handle the index arrays 
in advance. An example will show it best:

index = array([1, 2, 4, 2, 3, 1])   # 1 and 2 occur twice
data = array([1, 1, 1, 1, 1, 1])
newindex, newdata = filter_and_add(index, data)  # the kind of function I mean
print newindex
--> array([1, 2, 4, 3])  # duplicates have been removed
print newdata
--> array([2, 2, 1, 1])  # corresponding entries have been added
a[newindex] += newdata


Take Surveys. Earn Cash. Influence the Future of IT
Join's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash

More information about the Numpy-discussion mailing list