Using C/C++ for heavy calculations in Python (Also MySQL)

Question:

I’m implementing an algorithm into my Python web application, and it includes doing some (possibly) large clustering and matrix calculations. I’ve seen that Python can use C/C++ libraries, and thought that it might be a good idea to utilize this to speed things up.

First: Are there any reasons not to, or anything I should keep in mind while doing this?

Second: I have some reluctance against connecting C to MySQL (where I would get the data the calculations). Is this in any way justified?

Asked By: oyblix

||

Answers:

Not the answer you expected, but i have been down that road and advise KISS:

  • First make it work in the most simple way possible.
  • Only than look into speeding things up later / complicating the design.

There are lots of other ways to phrase this such as “do not fix hypothetical problems unless resources are unlimited“.

Answered By: Barry Staes

Use the ecosystem.

For matrices, using numpy and scipy can provide approximately the same range of functionality as tools like Matlab. If you learn to write idiomatic code with these modules, the inner loops can take place in the C or FORTRAN implementations of the modules, resulting in C-like overall performance with Python expressiveness for most tasks. You may also be interested in numexpr, which can further accelerate and in some cases parallelize numpy/scipy expressions.

If you must write compute-intensive inner loops in Python, think hard about it first. Maybe you can reformulate the problem in a way more suited to numpy/scipy. Or, maybe you can use data structures available in Python to come up with a better algorithm rather than a faster implementation of the same algorithm. If not, there’s Cython, which uses a restricted subset of Python to compile to machine code.

Only as a last resort, and after profiling to identify the absolute worst bottlenecks, should you consider writing an extension module in C/C++. There are just so many easier ways to meet the vast majority of performance requirements, and numeric/mathematical code is an area with very good existing library support.

Answered By: musicinmybrain

cython support for c++ is much better than what it was. You can use most of the standard library in cython seamlessly. There are up to 500x speedups in the extreme best case.

My experience is that it is best to keep the cython code extremely thin, and forward all arguments to c++. It is much easier to debug c++ directly, and the syntax is better understood. Having to maintain a code base unnecessarily in three different languages is a pain.

Using c++/cython means that you have to spend a little time thinking about ownership issues. I.e. it is often safest not to allocate anything in c++ but prepare the memory in python / cython. (Use array.array or numpy.array). Alternatively, make a c++ object wrapped in cython which has a deallocation function. All this means that your application will be more fragile than if it is written only in python or c++: You are abandoning both RAII / gc.

On the other hand, your python code should translate line for line into modern c++. So this reminds you not to use old fashioned new or delete etc in your new c++ code but make things fast and clean by keeping the abstractions at a high level.

Remember too to re-examine the assumptions behind your original algorithmic choices. What is sensible for python might be foolish for c++.

Finally, python makes everything significantly simpler and cleaner and faster to debug than c++. But in many ways, c++ encourages more powerful abstractions and better separation of concerns.

When you programme with python and cython and c++, it slowly comes to feel like taking the worse bits of both approaches. It might be worth biting the bullet and rewriting completely in c++. You can keep the python test harness and use the original design as a prototype / testbed.

Answered By: Leo Goodstadt

yes we can do it by creating tables for storage of the operation or value of certain variables like value of pi , e , sin etc . the write the program in the python and give necessary commands for the mathematical operation . by using the the mysql connector we can access the certain variables in the operations.

Answered By: Shivansh Malhotra
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.