File: compiled_code.rst

package info (click to toggle)
scipy 1.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 132,464 kB
  • sloc: python: 207,830; ansic: 92,105; fortran: 76,906; cpp: 68,145; javascript: 32,742; makefile: 422; pascal: 421; sh: 158
file content (52 lines) | stat: -rwxr-xr-x 1,975 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
:orphan:

.. _other-languages:

=============
Beyond Python
=============

This is a small collection of thoughts related to the inclusion of code written
in languages other than Python. Currently, the only option for languages other
than Python that is explicitly documented is :ref:`Cython<adding-cython>`.

*Can I use a programming language other than Python to speed up my code?*

Yes. The languages used in SciPy are Python, Cython, C, C++, and Fortran. All
of these have their pros and cons. If Python really doesn't offer enough
performance, one of those languages can be used. Important concerns when
using compiled languages are maintainability and portability. For
maintainability, Cython is clearly preferred over C/C++/Fortran. Cython and C
are more portable than C++/Fortran. A lot of the existing C and Fortran code
in SciPy is older, battle-tested code that was only wrapped in (but not
specifically written for) Python/SciPy. Therefore, the basic advice is: use
Cython. If there are specific reasons why C/C++/Fortran should be preferred,
please discuss those reasons first.

*Can I use Numba or Pythran?*

Not yet, but we're considering it for the future.

*How do I debug code written in C/C++/Fortran inside SciPy?*

The easiest way to do this is to first write a Python script that
invokes the C code whose execution you want to debug. For instance
``mytest.py``::

    from scipy.special import hyp2f1
    print(hyp2f1(5.0, 1.0, -1.8, 0.95))

Now, you can run::

    gdb --args python runtests.py -g --python mytest.py

If you didn't compile with debug symbols enabled before, remove the
``build`` directory first. While in the debugger::

    (gdb) break cephes_hyp2f1
    (gdb) run

The execution will now stop at the corresponding C function and you
can step through it as usual. Instead of plain ``gdb`` you can, of
course, use your favorite alternative debugger; run it on the
``python`` binary with arguments ``runtests.py -g --python mytest.py``.