2

I started to read the book "numerical recipes in C"... I try make my programs more efficients and faster... So, is 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?

fpointbin
  • 1,633
  • 3
  • 16
  • 20
  • 1
    If you value efficiency, the poor algorithms in NR will probably hurt you a lot more than your choice of compiler: http://www.uwyo.edu/buerkle/misc/wnotnr.html – Ken Mar 13 '11 at 03:05
  • 5
    "I know that C++ is a super-set of C..." This is incorrect. I would love that myth to just go away. http://www.research.att.com/~bs/bs_faq.html#C-is-subset – Travis Gockel Mar 13 '11 at 03:14
  • 1
    C++ is not a superset of C. But `cmath` is a wrapper of `math.h` (apart from a few minor compatibility changes). It's included for compatibility with C. – Lightness Races in Orbit Mar 13 '11 at 03:34
  • 1
    @Travis I wonder if you actually understood your citation, which is intended to make the point that, as a matter of practice, rather than strict logic, C++ is a superset of C -- and that the author of that citation went to considerable lengths to make that be so. Here's a bit that perhaps you didn't read far enough to encounter: "Except for a few examples such as the ones shown above (and listed in detail in the C++ standard and in Appendix B of The C++ Programming Language (3rd Edition)), C++ is a superset of C." – Jim Balter Mar 13 '11 at 08:51
  • @Jim: Yes, I understand that C++ is *almost* a super set of C, it was a design goal. But "almost super set" doesn't really have a definition and if you're worried about the intricate little details (as you would be if you're talking about how choice of language would effect the speed), then those definitions matter. – Travis Gockel Mar 13 '11 at 18:04
  • 1
    @Travis No, on the issue of speed, the details in which C++ is not a superset of C do not matter a whit -- such details as enums being separate types, sizeof('a') == 1, int foo() having no parameters rather than unknown parameters, etc. – Jim Balter Mar 13 '11 at 18:50
  • So... c++ Is more than a super-set, doing abuse of the words... But, mathematically, we guess this: "We've got a set called A( C language ) and a set called B( C++ language )... if A has got an element that can't be supported by B set, we conclude that B isnt a super set of A... C language admit int class = 1, but C++ dont, so C++ cant be a super set of C" – fpointbin Mar 13 '11 at 19:06
  • @Jim: You're arguing semantics and missing my point. By mathematical definition, C++ is *not* a superset of C. I agree that C++ is *almost* a superset of C, but that definition has *no relevant meaning*. C and C++ are two separate languages and should be thought of that way. You can identify language elements that would not effect performance in this instance, but there are occasions where one language will be exhibit different performance characteristics than the other. I wouldn't want leave fbin with the impression that they will always be the same, because they won't. – Travis Gockel Mar 13 '11 at 21:47
  • @Jim what d'you think 'bout my "demonstration"? is it enough? – fpointbin Mar 13 '11 at 22:04
  • @Travis "You're arguing semantics and missing my point." No, what **I** am doing is failing to be intellectually dishonest. – Jim Balter Mar 14 '11 at 15:47
  • @Jim: What am I being intellectually dishonest about? C++ is not a superset of C. C and C++ are two separate languages. Separate languages exhibit separate characteristics, sometimes in ways we do not expect. What am I missing? – Travis Gockel Mar 14 '11 at 18:03
  • @Travis You: "if you're worried about the intricate little details (as you would be if you're talking about how choice of language would effect the speed)" Me: "do not matter a whit" You: "missing my point" Me: Suck it. – Jim Balter Mar 14 '11 at 19:29

4 Answers4

7

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.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
2

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.

phooji
  • 10,086
  • 2
  • 38
  • 45
  • "C++ is a superset of C." [No, it's not.](http://www.research.att.com/~bs/bs_faq.html#C-is-subset) – Lightness Races in Orbit Mar 13 '11 at 03:35
  • @Tomalak: I've removed the offending statement. – phooji Mar 13 '11 at 03:55
  • You should avoid using virtual functions where they are not needed. :-) When you use virtual functions in C++, you would probably have to use function pointers in C to get the same effect. That wouldn't make the code any faster! – Bo Persson Mar 13 '11 at 07:00
  • @Tomalak From your source: "Except for a few examples such as the ones shown above (and listed in detail in the C++ standard and in Appendix B of The C++ Programming Language (3rd Edition)), C++ is a superset of C." – Jim Balter Mar 13 '11 at 08:54
  • @phooji Bjarne Stroustrup would not have been offended by your statement -- he agrees with it in essence, and went to great lengths and considerable sacrifice to make it so. – Jim Balter Mar 13 '11 at 08:55
  • @Jim: I do not take "except for the ways in which C++ is not a superset of C, C++ is a superset of C" as a citation for "C++ is a superset of C", especially when the same source states categorically that in the mathematical sense the assertion is not true. Also, I don't really care what Bjarne is and is not offended by: he hasn't been the one true authoritative source on C++ for over a decade. – Lightness Races in Orbit Mar 13 '11 at 14:23
  • @Jim and Tomalak: Thanks for the discussion. I do math for a living, so I appreciate people's concern with formal-sounding-but-technically-incorrect statements (like the 'subset' claim). To see me be very pedantic elsewhere, just have a look at some of the recent 'what is the big-O complexity of this' type questions. In this case, based on idiomatic use of C vs. idiomatic use of C++, I don't think it is wrong to agree with an OP who makes the 'subset' claim, although it should probably be pointed out to them anyway. – phooji Mar 13 '11 at 17:34
  • @phooji It is indeed pedantic; it is in fact intellectually dishonest. The OP's statement was correct for the relevant practical purposes of the question at hand. – Jim Balter Mar 13 '11 at 18:51
  • @phooji BTW, Stroustrup does not state it "categorically", he states it with qualification: "In the strict mathematical sense" -- that's the opposite of "categorically". And how droll that someone cites Stroustrup as an authority on this matter and then immediately declaims that authority when it suits him. But I'm already used to such behavior from that contributor. – Jim Balter Mar 13 '11 at 19:00
  • @Jim Balter: In the strict mathematical sense, the subset claim is correct. Both C and C++ are turing-complete, and one is fully expressible in the other. In that sense, C is a subset of C++ and C++ is a subset of C. At a syntactic level, the situation may be more complicated, but most 'not a subset' claims seem to rely on language features like initializers (i.e., semantics), which inevitably boils down to 'how much rewriting am I allowed to do during my translation from C to C++.' – phooji Mar 13 '11 at 22:17
  • @phooji: turing-completeness does not apply - the common meaning of 'language A is a subset/superset of language B' compares the sets of syntactic and semantic rules; otherwise, the statement would be a tautology in the class of turing-complete languages and any such comparison pointless... – Christoph Mar 14 '11 at 10:39
  • @Christoph: Most examples of 'C++ not superset of C' include language interpretation/syntax rather than just syntax. My point is exactly what you said: if you include language feature semantics in the argument without specifying allowable syntactic translations, then the whole argument boils down to turing-completeness and is thus pointless. – phooji Mar 14 '11 at 15:24
  • @phooji: whoops make that: "interpretation/semantics rather than just syntax." – phooji Mar 14 '11 at 16:07
1

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.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
1

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466