[Python-ideas] Marvelous movable memoryview

Sturla Molden sturla at molden.no
Sat Jul 28 23:09:13 CEST 2012


Den 27.07.2012 14:26, skrev Christian Heimes:
> The new buffer interface and memoryview's are already very complex. I 
> haven't found a library yet that supports all edge cases. Even NumPy 
> doesn't support multidimensional and non-contiguous buffer with 
> suboffsets. I wanted to use a sub-offset of 3 on the last dimension 
> with a negative stride on the last dimension to convert BGR to RGB 
> inside the buffer code. It didn't work because NumPy ignore the 
> information.

For those who write C extensions with Cython, it might be useful to know 
that it can compile "Python-like" code that uses PEP 3118 buffers to 
very efficient C code:

     http://docs.cython.org/src/userguide/memoryviews.html

On a simple test posted to the cython-user list,

     https://github.com/jakevdp/memview_benchmarks

I only got 2.2 % increased run-time compared to plain C pointer 
arithmetics. That involved creating two million PEP 3118 buffer objects 
and computing one million dot products with length 1000.

This takes most of the complexity of using PEP 3118 buffers in C 
extensions away, and allows us to write C extensions with a syntax that 
are very similar to Python with NumPy arrays.

(On the other hand, the old "NumPy array syntax" in Cython is 
considerably slower, particularly when slicing the arrays.)


Sturla



More information about the Python-ideas mailing list