0% found this document useful (0 votes)
42 views7 pages

Using Cython To Speed Up Numerical Python Programs

Cython is an extension of Python that allows Python code to be compiled into C/C++ extensions for increased performance in numerical programs. The document discusses how Cython can be used to speed up a Python program for numerical integration by 30x by declaring variable types. Cython provides a user-friendly way to integrate Python and C/C++ code by compiling Python code directly to machine code while maintaining access to Python libraries and functions.

Uploaded by

Saraswathi A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Using Cython To Speed Up Numerical Python Programs

Cython is an extension of Python that allows Python code to be compiled into C/C++ extensions for increased performance in numerical programs. The document discusses how Cython can be used to speed up a Python program for numerical integration by 30x by declaring variable types. Cython provides a user-friendly way to integrate Python and C/C++ code by compiling Python code directly to machine code while maintaining access to Python libraries and functions.

Uploaded by

Saraswathi A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

USING CYTHON TO SPEED UP NUMERICAL PYTHON PROGRAMS

Title : Using Cython to Speed up Numerical Python Programs


Author : Ilmar M. Wilbers , Hans Petter Langtangen , and Asmund OdegArd
Year : July, 2009

1
INTRODUCTION:
•There is one major concern when using Python for scientific computations, namely the possible
loss of computational speed.
•Cython is a new extension of the Python language that aims at making the integration of C
and Python.
•Cython does not simply translate Python code to C code. Instead, it uses the Python run-time
environment, compiling everything directly to machine code.

2
CYTHON EXAMPLE:
•The following is simple code for numerical integration using the Trapezoidal rule. The Python
version looks like:

3
CYTHON EXAMPLE:
•This code runs fine with Cython, but in order to get a speed-up, we need to statically declare types
using the cdef keyword.
•The following Cython code runs about 30 times faster than the Python code:

4
CYTHON EXAMPLE:
•Functions defined with def and cdef can call each other within the Cython code, but functions defined
with cdef cannot be called directly from Python.
•So one either has to use a function defined with the keyword def, or another keyword cpdef indicating
that the function is to be used both as a Python and a Cython function.

5
CONCLUSION:
•Cython is a new, user-friendly alternative to mixing Python with Fortran, C, or C++ code.
• These constructs automatically move large parts of the calculation to assembly code.
•The performance is almost as good as that of using Fortran, C, or C++ from Python, but
Cython seems considerably easier to work with.

6
REFERENCES:
• D. Beazley et al. SWIG: Simplified wrapper and interface generator http://www.swig.org.
• Boost C++ libraries http://boost.org/libs.
• X. Cai and H. P. Langtangen Parallelizing PDE solvers using the Python programming language
In A. M. Bruaset and A. Tveito, editors, Numerical Solution of Partial Differential Equations on
Parallel Computers, volume 51 of Lecture Notes in Computational Science and Engineering, pages
295–325. Springer, 2006.
• X. Cai, H. P. Langtangen and H. Moe On the performance of the Python programming language
for serial and parallel scientific computations Scientific Programming, vol.13(1), 31–56, 2005.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy