Commit cfac0913 authored by Lars Buitinck's avatar Lars Buitinck

clarify memoryview example in userguide

parent 4b29260d
...@@ -6,9 +6,10 @@ ...@@ -6,9 +6,10 @@
Typed Memoryviews Typed Memoryviews
***************** *****************
Typed memoryviews can be used for efficient access to buffers, such as NumPy Typed memoryviews allow efficient access to memory buffers, such as those
arrays, without incurring any Python overhead. Memoryviews are similar to the underlying NumPy arrays, without incurring any Python overhead.
current numpy array buffer support (``np.ndarray[np.float64_t, ndim=2]``), but Memoryviews are similar to the current NumPy array buffer support
(``np.ndarray[np.float64_t, ndim=2]``), but
they have more features and cleaner syntax. they have more features and cleaner syntax.
Memoryviews are more general than the old numpy aray buffer support, because Memoryviews are more general than the old numpy aray buffer support, because
...@@ -26,27 +27,20 @@ Quickstart ...@@ -26,27 +27,20 @@ Quickstart
:: ::
# Import cython view array to make Cython arrays
from cython.view cimport array as cvarray from cython.view cimport array as cvarray
import numpy as np import numpy as np
# A numpy array # Memoryview on a NumPy array
narr = np.arange(27).reshape((3,3,3)) narr = np.arange(27).reshape((3,3,3))
# A memoryview round the numpy array
cdef int [:, :, :] narr_view = narr cdef int [:, :, :] narr_view = narr
# A C array # Memoryview on a C array
cdef int carr[3][3][3] cdef int carr[3][3][3]
# A memoryview round the C array
cdef int [:, :, :] carr_view = carr cdef int [:, :, :] carr_view = carr
# A Cython array # Memoryview on a Cython array
cyarr = cvarray(shape=(3, 3, 3), itemsize=sizeof(int), format="i") cyarr = cvarray(shape=(3, 3, 3), itemsize=sizeof(int), format="i")
# A memoryview round the Cython array
cdef int [:, :, :] cyarr_view = cyarr cdef int [:, :, :] cyarr_view = cyarr
# Show the sum of all the arrays before altering it # Show the sum of all the arrays before altering it
...@@ -65,7 +59,7 @@ Quickstart ...@@ -65,7 +59,7 @@ Quickstart
carr_view[0, 0, 0] = 100 carr_view[0, 0, 0] = 100
cyarr_view[0, 0, 0] = 1000 cyarr_view[0, 0, 0] = 1000
# Altering the memoryview of the Numpy array altered the contents in-place # Assigning into the memoryview on the Numpy array alters the latter
print "Numpy sum of Numpy array after assignments:", narr.sum() print "Numpy sum of Numpy array after assignments:", narr.sum()
# A function using a memoryview does not usually need the GIL # A function using a memoryview does not usually need the GIL
...@@ -80,16 +74,12 @@ Quickstart ...@@ -80,16 +74,12 @@ Quickstart
total += arr[i, j, k] total += arr[i, j, k]
return total return total
# A function accepting a memoryview knows how to use a Numpy array # A function accepting a memoryview knows how to use a Numpy array,
# a C array, a Cython array...
print "Memoryview sum of Numpy array is", sum3d(narr) print "Memoryview sum of Numpy array is", sum3d(narr)
# And a C array
print "Memoryview sum of C array is", sum3d(carr) print "Memoryview sum of C array is", sum3d(carr)
# And a Cython array
print "Memoryview sum of Cython array is", sum3d(cyarr) print "Memoryview sum of Cython array is", sum3d(cyarr)
# ... and of course, a memoryview.
# And of course, a memoryview
print "Memoryview sum of C memoryview is", sum3d(carr_view) print "Memoryview sum of C memoryview is", sum3d(carr_view)
This code gives output:: This code gives output::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment