P. Camilleri
P. Camilleri

Reputation: 13218

Why are there two syntaxes to pass np arrays to cython function?

More specifically, what is the difference between:

def f(double[::1, :]):
    pass

and

def f(np.ndarray[double, ndim=2, order='fortran']):
    pass

?

Upvotes: 1

Views: 42

Answers (1)

JoshAdel
JoshAdel

Reputation: 68722

The first is the newer "typed memoryview" style:

http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html

The second is the older buffer syntax:

http://cython.readthedocs.io/en/latest/src/tutorial/numpy.html#efficient-indexing

See the documentation on typed memoryviews for a comparison since it covers the differences pretty well.

The one thing that I don't is mentioned in the docs is that sometimes I found historically that the buffer syntax could be a little faster when passing arrays to functions that don't take much time to compute since acquiring the memoryview had a little bit more overhead, although it was more efficient when accessing data. I'm not sure if this is still the case, but it's probably worth benchmarking for your particular use case.

Upvotes: 2

Related Questions