I'm new to programming and was wondering how I can have a python program execute and communicate with a c program. I am doing a mathematical computation in python, and was wondering if I could write up the main computation in C, that way the computation runs faster. I've been reading about "calling c functions from python", "including C or C++ code directly in your Python code", and "using c libraries from python". Is this the same thing? I want a python program to execute a c program and receive the results.
What does it mean to "call C library functions" from python? Would it allow the python script to use c libraries or allow the script to execute code within a c compiler?
thanks
There's also numpy which can be reasonably fast when dealing with "array operations" (sometimes called vector operations, but I find that term confusing with SIMD terminology).  You'll probably need numpy if you decide to go the cython route, so if the algorithm isn't too complicated, you might want to see if it is good enough with numpy by itself first.
Note that there are two different routes you can take here.  You can use subprocess which basically issues system calls to some other program that you have written.  This is slow because you need to start a new process and send the data into the process and then read the data back from the process.  In other words, the data gets replicated multiple times for each call.  The second route is calling a C function from python.  Since Cpython (the reference and most common python implementation) is written in C, you can create C extensions.  They're basically compiled libraries that adhere to a certain API.  Then Cpython can load those libraries and use the functions inside, passing pointers to the data.  In this way, the data isn't actually replicated -- You're working with the same block of memory in python that you're using in C.  The downside here is that the C API is a little complex.  That's where 3rd party extensions and existing libraries come in (numpy, cython, ctypes, etc).  They all have different ways of pushing computations int C functions without you having to worry about the C API.  Numpy removes loops so you can add, subtract, multiply arrays quickly (among MANY other things).  Cython translates python code to C which you can then compile and import -- typically to gain speed here you need to provide additional hints which allow cython to optimize the generated code, ctypes is a little fragile since you have to re-specify your C function prototype, but otherwise it's pretty easy as long as you can compile your library into a shared object ... The list could go on.
Also note that if you're not using numpy, you might want to check out pypy.  It claims to run your python code faster than Cpython.
executing c program from a python program