[Numpy-discussion] Optimizing a pure Python Workaround

Robert Kern robert.kern@gmail....
Mon Jul 13 23:26:01 CDT 2009


On Mon, Jul 13, 2009 at 18:11, Ian Mallett<geometrian@gmail.com> wrote:
> Hello,
>
> I have some code that makes vertex buffer object terrain.  Because the setup
> for this (a series of triangle strips) is a bit clunky, I just implemented
> the tricky parts in Python.
>
> The code works, but it's slow.  How should I go about optimizing it?
>
> Thanks,
>
> Ian
>
> size = [#int_something,#int_something_else]
> fsize = map(float,size)
> array = []
> for x in xrange(size[0]):
>     for y in xrange(size[1]):
>         array.append(np.array([ x   /fsize[0],y/fsize[1],0.0],"f"))
>         array.append(np.array([(x+1)/fsize[0],y/fsize[1],0.0],"f"))
> array = np.array(array)
> #"array" is the final product.  I need this final product generated more
> quickly.

Untested:

x = np.arange(size[0])
x2 = np.column_stack([x, x+1]).reshape([-1,1])
x3 = x2.repeat(size[1], axis=1).flatten()
y = np.arange(size[1]).repeat(size[0] * 2)

array = np.zeros([len(y), 3])
array[:,0] = x3
array[:,1] = y

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list