C++ vs C for distributed computing [closed]
I am about to start a new project in scientific computing, which will run on a computer cluster using from 8 up to 32 CPUs. I will use MPI to distribute the calculations. The last thing is which language is better to use C or C++? I will do some serious number crunching, and I will need a smooth and well-distributed parallelization. I have no experience using MPI, but I am aware that it suits C much more than C++, even if libraries like boost::MPI are used. Taking into account that performance is crucial, what language is better to use?
Note: Of course, one could write C-style code on C++, but that wouldn't be C++ after all. When I speak of C++ I mean to use lots of OOP and generic programming.
You can write slow code in any language. MPI is a message-passing library so it simply does not care whether you call it from C or C++ --- use whichever language you are better at.
Which you should determine by profiling and measuring.
The Language Shootout comparison between C and C++ is pretty even about size, speed and memory use.
Lastly, there is also the underestimated importance of 'time to code'. You may get to a working program faster in C++ than in C simply because you have more high-level tools at your disposal. Or you may not, so I would try to measure.
When I speak of C++ I mean to use lots of OOP and generic programming.
This is probably a bad idea. C++ isn’t primarily an object-oriented language and using it as one “just because” isn’t a good approach. C++ shines much more in generic programming.
Especially if performance is important for you, go with C++, it’s faster than C while still allowing high abstraction if properly used, but don’t use OOP. For a way how C++ can be used much better, have a look at the <algorithms>
standard header.
No-one can really answer this except you. Do you really need OOP if most of your code will be number crunching?
My vote would be for C as all you need is number crunching and no need for fancy libraries to pass objects around and such. Boost is not that much faster for MPI anyways.
You can write efficient code or horrible code in either language. Your best bet will most likely be to stick with whatever language you are most comfortable with. Your programming skill will likely give a greater benefit than language choice (assuming you have access to an equally-capable compiler for both languages).
If all else fails, don't forget that you can use both C and C++ in your system. You can write your number-crunching algorithms in C, build it into a library, and then link that library into a C++ application.
I'd always use C in this kind of app. Just mho.
精彩评论