C vs C++ numerical recipes
I started to read the book "numerical recipes in C"... I try make my programs more efficients and faster... So, is开发者_StackOverflow中文版 the same thing, to use C or C++? I know that C++ is a super-set of C... But, are there differences between "cmath" library (c++) with "math.h" library(c)? It will intervene in the velocity of execution?... in order to that... i can use C++ without any difference with a C program?
First off, C++ is not a superset of C. Though there are many similarities, there are also differences, and neither is a subset of the other.
Now that we have that out of the way, Numerical Recipes uses an especially simple subset of C, and you should not encounter many difficulties in using the code in a C++ program. Most of the algorithms will Just Work without modification[1].
You shouldn't worry about details like <cmath>
; on most platforms, it's just a thin shim over the C math headers, and does not introduce any overhead. Broadly, this is true of C++ in general: when you're writing code that looks like C code, little or no additional overhead is introduced.
[1] In as much as they work in C; Numerical Recipes is a useful reference, but the programs contained therein are not entirely bug-free, nor are all of the algorithms state of the art. Some of the algorithms are numerically poor choices. If you become seriously interested in any of the topics discussed, be sure to read the references, and then to look for more current material on the subject.
C++ sometimes gets a bad rep for being less efficient and/or more bloated than plain C. Certainly there are more ways to write inefficient code -- you should avoid having virtual function calls in tight inner loops, for example.
With that said, for basic numerics code, I would not expect a great deal of difference between C and C++. Once you start looking at scientific applications, you can get a lot of mileage out of C++ template metaprogramming for better efficiency (and, at the same time, more readable code, although compilation errors are a bit gnarly). A good example of this is the Blitz++ library -- it's explicitly designed to compete low-level code (in this case Fortran).
Edit: Removed subset/superset related claims.
Should be exactly the same. If you want to see what you can get from C++ numerically, you can look at template meta-programming numerical algorithms -- they work when you know enough information at compile time to do some of the computing during build.
Of course, it depends on your specific compiler, etc. but my view is that you should use C++, and that it should perform at least as fast or faster (due to more modern optimizations) than C.
Of course, C++ provides a more functionality, which is why I recommend it. And if you use that functionality, that may possibly impact performance. But C++ is still very fast (faster than most languages in use today). And if you don't need those advanced features, don't use them.
精彩评论