Message from discussion
Trying to speed up numpy array loop, getting a ValueError
Received: by 10.100.23.30 with SMTP id 30mr1749089anw.27.1307172149823;
Sat, 04 Jun 2011 00:22:29 -0700 (PDT)
X-BeenThere: cython-users@googlegroups.com
Received: by 10.91.196.22 with SMTP id y22ls447351agp.5.gmail; Sat, 04 Jun
2011 00:22:28 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.91.33.25 with SMTP id l25mr315290agj.47.1307172148614; Sat, 04
Jun 2011 00:22:28 -0700 (PDT)
Received: by r20g2000yqd.googlegroups.com with HTTP; Sat, 4 Jun 2011 00:22:28
-0700 (PDT)
Date: Sat, 4 Jun 2011 00:22:28 -0700 (PDT)
In-Reply-To: <9df0cdb7-c916-4980-934f-025bfd478936@gv8g2000vbb.googlegroups.com>
References: <9df0cdb7-c916-4980-934f-025bfd478936@gv8g2000vbb.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11,gzip(gfe)
Message-ID: <75eeb3c1-59d1-4ce8-b628-8c2a8c545bf2@r20g2000yqd.googlegroups.com>
Subject: Re: Trying to speed up numpy array loop, getting a ValueError
From: Jon Olav Vik <jono...@gmail.com>
To: cython-users <cython-users@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Jun 3, 11:43=A0am, tastygroove <bel...@gmail.com> wrote:
> I am new to Cython, so I apologize in advance if I am doing something
> dumb.
Conversely, my apologies if I completely misunderstood your problem
8-)
> I am writing some imaging software, right now in pure Python, that
> includes several loops over (large) numpy arrays. These operations are
> painfully slow in pure Python, so I thought that I would use Cython to
> speed things up. =A0Some of the functions are stupid simple, so this
> should be a breeze. =A0No luck there so far. =A0Here is my problem.
>
> I have a simple function that takes two real values from a 4D array
> and stores them as real and imaginary parts of a complex valued 3D
> array. =A0I just have to loop over the input array and store the values
> in a new complex valued array. =A0Simple. =A0I have re-written the
> function in Cython, following the "Working with NumPy" tutorial.
If you're really going to be doing simple elementwise operations,
maybe simple Numpy operations will suffice? The following seems
equivalent to your code:
def f(model):
fcube =3D np.zeros(model.shape[1:], dtype=3Dnp.complex64)
fcube.real =3D model[1]
fcube.imag =3D model[2]
return fcube
In [11]: arrtest.f(np.arange(72.).reshape(3,2,3,4))
Out[11]:
array([[[ 24.+48.j, 25.+49.j, 26.+50.j, 27.+51.j],
[ 28.+52.j, 29.+53.j, 30.+54.j, 31.+55.j],
[ 32.+56.j, 33.+57.j, 34.+58.j, 35.+59.j]],
[[ 36.+60.j, 37.+61.j, 38.+62.j, 39.+63.j],
[ 40.+64.j, 41.+65.j, 42.+66.j, 43.+67.j],
[ 44.+68.j, 45.+69.j, 46.+70.j, 47.+71.j]]],
dtype=3Dcomplex64)
In [12]: arrtest.get_f_cube(np.arange(72.).reshape(3,2,3,4))
Out[12]:
array([[[ 24.+48.j, 25.+49.j, 26.+50.j, 27.+51.j],
[ 28.+52.j, 29.+53.j, 30.+54.j, 31.+55.j],
[ 32.+56.j, 33.+57.j, 34.+58.j, 35.+59.j]],
[[ 36.+60.j, 37.+61.j, 38.+62.j, 39.+63.j],
[ 40.+64.j, 41.+65.j, 42.+66.j, 43.+67.j],
[ 44.+68.j, 45.+69.j, 46.+70.j, 47.+71.j]]],
dtype=3Dcomplex64)
In [21]: a =3D np.arange(72.0).reshape(3,2,3,4)
In [22]: timeit arrtest.get_f_cube(a)
100000 loops, best of 3: 11.2 us per loop
In [23]: timeit arrtest.f(a)
100000 loops, best of 3: 6.38 us per loop
In [18]: a =3D np.arange(7200000.0).reshape(3,2,3,400000)
In [19]: timeit arrtest.get_f_cube(a)
1 loops, best of 3: 791 ms per loop
In [20]: timeit arrtest.f(a)
10 loops, best of 3: 31.1 ms per loop
You can see that the relative performance of Numpy is better with
large arrays. This is compiled from your code without any improvements
suggested in other posts.
I may not fully understand what your code is supposed to do -- but you
do realize that Python indexing is zero-based? Your code seems to
ignore the entire model[0].
Hope this helps,
Jon Olav