Richard said:
If you're using the same source code with identical semantics, it's true
that there's no particular reason for one to be faster than the other. But,
if you're using the same source code with identical semantics, why bother
with C++?
That's a good point. But, C++ has about 14 billion extra features
beyond that which C has. If restricting yourself to the C-like features
carries no performance penalty, doesn't it seem likely that at least a
few useful C++-only features have no performance penalty compared to
equivalent way of doing things in C?
For example, C++ allows you to do this:
int *i = new int[100];
There is no reason the performance of this should be any worse than
the C equivalent:
int *i = (int *) malloc (100 * sizeof (int));
But, which one is easier to write? Also, will the compiler catch
the error for you if you accidentally change the second one to this?
double *d = (double *) malloc (100 * sizeof (int));
Also templates carry no overhead compared to doing the equivalent
thing in C. In some cases, they can even make it easier to make
fast code. Say, for example, you need to implement sets of floats
and ints. With C, you write everything twice or you wastefully
store everything as void * values that point to the actual data.
C++ templates let you write the code only once and avoid the
performance penalties of dealing with pointers when you want to
deal directly with the native types. Of course, templates can
easily get out of control and lead to bloat, but they don't have
to if you use them wisely.
- Logan