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
ac8538fb
Commit
ac8538fb
authored
Jan 19, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove initial 'calling external C functions' tutorial - it's broken beyond repair
parent
5655b446
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
53 deletions
+0
-53
src/tutorial/external.rst
src/tutorial/external.rst
+0
-52
src/tutorial/index.rst
src/tutorial/index.rst
+0
-1
No files found.
src/tutorial/external.rst
deleted
100644 → 0
View file @
5655b446
Calling external C functions
============================
It is perfectly OK do ``from math import sin`` to use Python's
``sin()`` function. However, calling C's own ``sin()`` function is
substantially faster, especially in tight loops. It can be declared
and used in Cython as follows::
cdef extern from "math.h":
double sin(double)
cdef double f(double x):
return sin(x*x)
At this point there are no longer any Python wrapper objects around
our values inside of the main for loop, and so we get an impressive
speedup to 219 times the speed of Python.
Note that the above code re-declares the function from ``math.h`` to
make it available to Cython code. The C compiler will see the
original declaration in ``math.h`` at compile time, but Cython
does not parse "math.h" and requires a separate definition.
When calling C functions, one must take care to link in the appropriate
libraries. This can be platform-specific; the below example works on Linux
and Mac OS X::
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules=[
Extension("demo",
["demo.pyx"],
libraries=["m"]) # Unix-like specific
]
setup(
name = "Demos",
cmdclass = {"build_ext": build_ext},
ext_modules = ext_modules
)
If one uses the Sage notebook to compile Cython code, one can use a special
comment to tell Sage to link in libraries::
#clib: m
Just like the ``sin()`` function from the math library, it is possible
to declare and call into any C library as long as the module that
Cython generates is properly linked against the shared or static
library.
src/tutorial/index.rst
View file @
ac8538fb
...
@@ -4,7 +4,6 @@ Tutorials
...
@@ -4,7 +4,6 @@ Tutorials
.. toctree::
.. toctree::
:maxdepth: 2
:maxdepth: 2
external
clibraries
clibraries
cdef_classes
cdef_classes
pxd_files
pxd_files
...
...
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