DEV Community

Mohammad Nazmul Hossain
Mohammad Nazmul Hossain

Posted on

C for scientific programming

My take on scientific programming is that I think of C as one of many tools in my toolkit for performing computational tasks in my scientific work. I wouldn’t necessarily suggest only programming in C. On the other hand, I would recommend taking advantage of C when the situation calls for it. In our lab, we use Python, R, (sometimes Matlab but increasingly less often), and when we feel the need, the need for speed, we use C.

Interactive data exploration#
For interactive data exploration, like when you want to load in some data, plot it in different ways, do some rudimentary calculations, plot the results, etc., then C may not be the best choice. For this sort of interactive exploratory scripting, a language like Python, Matlab, R, etc., may be entirely sufficient. In particular, these other languages make it very easy to generate great-looking graphics quickly.

Processing large data#
For cases where you need to process a large amount of data, you will find that these languages are slow. Even for fairly common statistical procedures like bootstrapping (techniques that involve resampling thousands or tens of thousands of times), interpreted languages will be orders of magnitude slower than C.

This is the situation when C starts to become very attractive. If you have a data processing operation or a simulation, and you know it will take a long time to run, then it is often worth it to spend some time implementing it in C. The graph below compares the speed of interpreters for several languages. As you can see, C shines in this regard.

Image description
My rule of thumb is that if I have to wait more than about 10 seconds to see the result of a calculation or operation, then I get annoyed, and I think about implementing it in C.

You might think, who cares if my calculation takes 10 seconds, or 30 seconds, not 5 minutes, for that matter? Are 5 minutes so bad? The answer is, no, it’s not so bad if you only have to do it once… but it’s almost never the case that you perform a computation on your data only once.

Top comments (3)

Collapse
 
orgarten profile image
Orell Garten

C++ is a much better choice for scientific computing. There are plenty of libraries that are written in C++ and there are so many compile time feature now that can be "easily" used.

Anyway, a lot of code in scientific computing is changed all the time, especially if it's actual part of the research and python allows for much faster iteration.

Collapse
 
pauljlucas profile image
Paul J. Lucas

For numerical calculations, C++ is just as fast and a much more capable language than C.

Collapse
 
drsensor profile image
૮༼⚆︿⚆༽つ

If you need parallelism or decent distributed computation then Julia is pretty good.