Fully typed cythonized code slower than pure numpy?

25 views
Skip to first unread message

Federico Vaggi

unread,
Apr 3, 2012, 6:15:45 PM4/3/12
to cython-users
Hi everyone,

I was trying to implement a pairwise distance function between an Nx2
vector and an Mx2 vector in Python. As this part is quite critical in
my code, I tried a few different approaches:

I put all my code in here:

https://bitbucket.org/FedericoV/numpy-tip-complex-modeling/src/29292a93cbf2/src/simulations

The cython_arc_distance.pyx implementation is a fair bit slower than
the numpy implementation using broadcasting. To check out the
benchmark, just pull down the repo with hg and run Benchmark.py after
building the cython file.

Any obvious speed ups I'm missing?

Thank you very much,

Federico

Chris Barker

unread,
Apr 4, 2012, 1:33:42 AM4/4/12
to cython...@googlegroups.com
On Tue, Apr 3, 2012 at 3:15 PM, Federico Vaggi <vaggi.f...@gmail.com> wrote:

> I was trying to implement a pairwise distance function between an Nx2
> vector and an Mx2 vector in Python.  As this part is quite critical in
> my code, I tried a few different approaches:

numpy may be about as fast as you can do anyway, but.... I fyou're
doing the same number of loops, operations and memeory allocations,
you're not going to do better than numpy (not that I've looked closely
enough at your problem...)

> https://bitbucket.org/FedericoV/numpy-tip-complex-modeling/src/29292a93cbf2/src/simulations

> Any obvious speed ups I'm missing?

it does look about right, but a couple things to consider:

1) have you looked at the annotated source (html) you can find hidden
python code where you think it souold be pure C.

2) you can use an unsigned type for your indexes -- i and j -- that can help.

3) you can replace "**2" with a multiply -- I"m not sure if Cython or
the C complier optimizes that for you.

HTH,
-Chris

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

刘振海

unread,
Apr 4, 2012, 7:34:42 AM4/4/12
to cython...@googlegroups.com
Hi,
I tried the cython version of function in my computer,it indeed slower than numpy broadcast version.
Then I replace "**2" with a multiply, then the cython version is faster.
I also try the memoryslice type (new in cython0.16), when I turn the data type to DTYPE[:,::1], it can run even faster.

ps: there is a little mistake in the cython version 
phi_1 = a[i,0] should be phi_1 = a[i,1], right?

cheers,
Liu Zhenhai
Reply all
Reply to author
Forward
0 new messages