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
2db3fb25
Commit
2db3fb25
authored
Dec 13, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clarification: prefer for-in-range over for-from
parent
9cd1b8c2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
14 deletions
+11
-14
docs/language_basics.rst
docs/language_basics.rst
+11
-14
No files found.
docs/language_basics.rst
View file @
2db3fb25
...
...
@@ -295,13 +295,21 @@ Python and C, and that Cython uses the Python precedences, not the C ones.
Integer for-loops
------------------
You should be aware that a for-loop such as
::
Cython recognises the usual Python for-in-range integer loop pattern
::
for i in range(n):
...
won't be very fast if ``i`` is not a :keyword:`cdef` integer type.
For iterating over ranges of integers, Cython has another form of for-loop::
If ``i`` is declared as a :keyword:`cdef` integer type, it will
optimise this into a pure C loop. This restriction is required as
otherwise the generated code wouldn't be correct due to potential
integer overflows on the target architecture. If you are worried that
the loop is not being converted correctly, use the annotate feature of
the cython commandline (``-a``) to easily see the generated C code.
See :ref:`automatic-range-conversion`
For backwards compatibility to Pyrex, Cython also supports another
form of for-loop::
for i from 0 <= i < n:
...
...
...
@@ -313,17 +321,6 @@ or::
where ``s`` is some integer step size.
If the loop variable and the lower and upper bounds are all C integers, this
form of loop will be much faster, because Cython will translate it into pure C
code.
.. note::
This is not necessary if ``i`` is a C integer type and ``n`` can be
determined at compile time. Just use the idiomatic :func:`range` loop, if
you are worried that the loop is not being converted correctly use the
annotate feature of the cython commandline (``-a``) to easily see the
generated C code. See :ref:`automatic-range-conversion`
Some things to note about the for-from loop:
* The target expression must be a variable name.
...
...
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