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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
79256a34
Commit
79256a34
authored
Jun 15, 2018
by
scoder
Committed by
GitHub
Jun 15, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2340 from gabrieldemarmiesse/pure_python_mode_6
Adding tests for "pure python mode" part 6
parents
01198ace
1d450aaa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
15 deletions
+17
-15
docs/examples/tutorial/pure/c_arrays.py
docs/examples/tutorial/pure/c_arrays.py
+15
-0
docs/src/tutorial/pure.rst
docs/src/tutorial/pure.rst
+2
-15
No files found.
docs/examples/tutorial/pure/c_arrays.py
0 → 100644
View file @
79256a34
import
cython
@
cython
.
locals
(
counts
=
cython
.
int
[
10
],
digit
=
cython
.
int
)
def
count_digits
(
digits
):
"""
>>> digits = '01112222333334445667788899'
>>> count_digits(map(int, digits))
[1, 3, 4, 5, 3, 1, 2, 2, 3, 2]
"""
counts
=
[
0
]
*
10
for
digit
in
digits
:
assert
0
<=
digit
<=
9
counts
[
digit
]
+=
1
return
counts
docs/src/tutorial/pure.rst
View file @
79256a34
...
...
@@ -378,22 +378,9 @@ Using C arrays for fixed size lists
Since Cython 0.22, C arrays can automatically coerce to Python lists or tuples.
This can be exploited to replace fixed size Python lists in Python code by C
arrays when compiled. An example:
:
arrays when compiled. An example:
import cython
@cython.locals(counts=cython.int[10], digit=cython.int)
def count_digits(digits):
"""
>>> digits = '01112222333334445667788899'
>>> count_digits(map(int, digits))
[1, 3, 4, 5, 3, 1, 2, 2, 3, 2]
"""
counts = [0] * 10
for digit in digits:
assert 0 <= digit <= 9
counts[digit] += 1
return counts
.. literalinclude:: ../../examples/tutorial/pure/c_arrays.py
In normal Python, this will use a Python list to collect the counts, whereas
Cython will generate C code that uses a C array of C ints.
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