Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
cfac0913
Commit
cfac0913
authored
Nov 23, 2012
by
Lars Buitinck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clarify memoryview example in userguide
parent
4b29260d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
21 deletions
+11
-21
docs/src/userguide/memoryviews.rst
docs/src/userguide/memoryviews.rst
+11
-21
No files found.
docs/src/userguide/memoryviews.rst
View file @
cfac0913
...
@@ -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 nump
y array
#
Memoryview on a NumP
y 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
# A
ltering the memoryview of the Numpy array altered the contents in-place
# A
ssigning 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::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment