3

I have heard about different ways to call C functions from Python code, such as ctypes, cython, swig, Boost.python, etc. Each has pros and cons, of course. My question is about efficiency. I need to call C numerical functions from Python. A typical example of such a C function is:

double f(double x){
  return sin(x)+cos(x)-pow(2,x) + x*x;
}

The invocation needs to be iterated 2000-200000 times in one run.

Under such context, which C->Python transformer should I use?

5
  • 6
    If that's the only thing you're moving to C then you're doing it wrong. Commented Dec 13, 2015 at 3:08
  • 1) So you have a loop in Python 200000 iterations and call a framework which calls this function. And that for every iteration? 2) Do you really have a run-time problem? Or do you just think you might have one? Commented Dec 13, 2015 at 4:18
  • Something like making f accept a range of doubles might help, like void f(int num, double* vals);. Computing this for a single double at a time is unlikely to help much at all. And as Olaf pointed out, this is best done after you have a measured problem. Commented Dec 13, 2015 at 7:05
  • 2
    Consider using numpy instead, which can do this kind of math on a whole array at once without per-item overhead. Commented Dec 13, 2015 at 8:29
  • The title of this question is too generic and a bit misleading. The question concerns itself specifically about math function calls. For common math functions like cos, sin, pow, etc, I would think that most high level languages leverage existing math libraries native to the OS platform. Those math libraries will be written in assembly and C. So Python and its various language implementations (C, Java, .NET, etc), bind to these libraries at the C/Assembly level and are already efficient. Commented Dec 13, 2015 at 8:30

1 Answer 1

5

Calling a function implemented in C will not magically make your program run faster. Not executing code written in Python might speed your program up. So you won't gain anything significant by moving such a simple function into a C routine. If you call it in a tight loop, the function itself might be fast but the overhead of the loop written in Python will completely dominate. So the general idea is to move tasks as large as possible into a C routine. In your example, this might be the entire loop. Once the chunk of work you do with a single call to a C function is large enough, the function call overhead itself will become negligible so you shouldn't worry about it that much. Call the function however you find it most convenient. Chances are high that it doesn't affect performance in a measurable way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.