Skip to content Skip to sidebar Skip to footer

Why Is My F2py Programs Slower Than Python Programs

I recently wrote a time consuming program with python and decided to rewrite the most time consuming part with fortran. However, the fortran code, wrapped with f2py, is slower than

Solution 1:

First, do this on the python code so you know exactly how it spends its time. Then, you can do a similar thing on the Fortran code using a debugger, if you like.

I suspect essentially all of the time goes into matrix operations, so any speed difference is due to the math library, not to the language that calls it. This post relays some of my experience doing that. Often the routines to do things like matrix multiplication, inverse, or Cholesky transform, are designed to be efficient on large matrices, but not on small.

For example, the LAPACK matrix-multiplication routine DGEMM has two character arguments, TRANSA and TRANSB, which can be upper or lower case, specifying whether each input matrix is transposed. To examine the value of those arguments, it calls a function LSAME. I found that, if I am spending a large fraction of my time multiplying small matrices, like 4x4, the program actually spends nearly all of its time calling LSAME, and very little time actually multiplying matrices. You can see how it would be easy to fix that.


Post a Comment for "Why Is My F2py Programs Slower Than Python Programs"