It seems that this problem should be posted at Python forum instead of a Fortran forum. But I think that most people at Python forum are not familiar to Fortran and asking here may be more appropriate.
The question is: when I want to speed up some procedures in Python, which one should I choose, C or Fortran?
(I want to focus on C vs Fortran and not consider other methods, e.g. numba, cython, etc )
In my experience, I prefer Fortran (with f2py) since it it more convenient to write. It is also faster in Fortran comparing to C, but I’m not sure if it just because I’m not very familiar to C. Consider that Python is implemented in C, I guess that maybe C is more suitable when working with Python in some situations.
I think that Fortran and C are not mutually exclusive with each other, i.e. I should choose Fortran in some situations and in others I should choose C. The problem is when should I choose Fortan, and when should I choose C?
If the procedures use numerical arrays, especially in two or more dimensions, it may be easier to code them in Fortran. Would it be easy to code them in Matlab? If the procedures are working with strings, doing lots of I/O, and connecting to other libraries, C may be easier.
Not only that, they are interoperable – there is a standardized way to call one from the other.
C++ and xtensor and pybind11: Xtensor is a multidimensional array libray for C++. Makes C++ more like numpy. Wrapping with pybind11 is pretty easy.
Drawback of (1) is that f2py doesn’t wrap all of Fortran. Drawback of (2) is that you have to write the Cython wrapper, which is a bit tedious. Drawback of (3) is that xtensor has some performance issues for certain problems..
Also, yes. You can use Fortran, C and C++ all in the exact same project. They are all interoperable.
To add to the discussion, the Fortran-C interoperability (and C++ if you do it correcty) revolves around two important things: BIND(C) and the ISO_C_BINDING module.
For intensive numerical computations, definitively Fortran. I write Fortran code and expose a C API with the iso_c_binding module.
I prefer writing by hand my Python wrappers over relying on Cython or f2py but that’s only a personal choice. An example on how it could be done, here.
you can also try gfort2py gfort2py, if you mainly use gfortran compiler to call Fortran code from python in an elegant way. See the example here: test.