Re: Generally, are the programs written by C++ slower than written by
C 10% ?
On Sep 2, 7:12 am, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
KaiWen <wenkai1...@gmail.com> wrote
Generally, are the programs written by C++ slower than written by
2. In runtime, the speed of the program written by C++ is <= the same
program written by C
This does not follow. Both languages are Turing complete and apply the
"do-not-pay-for-what's-not-used" policy, so if some program+input data
combination happens to be slower in one language on some platform, one
can always tune the code until the performance is the same.
Not necessarily, at least not practically. Tuning often
involves modifications in the data structures, and unless the
variables involved are private, the amount of work involved
generally makes the tuning impractical. If there's any chance
that performance will be an issue, you almost have to use C++,
rather than C, because it is very, very difficult to tune C.
For writing fast code in C++ one must be quite familiar with
the language so that one knows what's going on under the hood.
Yes and no. Most of the time, the largest speed gains are
achieved by using a different algorithm (which may require
diffrerent data structures to support it), or by caching data
(which, of course, only works if all accesses to the data go
through specified functions). Neither of these require any real
knowledge of what's going on under the hood.
Writing fast code in C is in principle easier as there are fewer high
level constructs which could potentially cause slowdowns.
Writing fast code in C is significantly harder, because of the
lack of encapsulation.
So, to give some concrete suggestions:
Large program, good C++ skills: use C++.
Small program, bad C++ skills: use C.
Large program, bad C++ skills: screwed both ways, use C++ to get a slow
and working program.
Small program, good C++ skills: doesn't matter, use C++ for convenience.
Speed important: use C++, regardless of the size.
Speed not important, large program: use C++.
Speed not important, (very) small program: use a more dynamic
Other issues may also be involved: C++ (used correctly) is far
more robust than most of the readily available alternatives, for
example; for a critical application, it's certainly preferrable
to C (or to Python, or to Java).
As for the level of C++ skills: if the solution is to use C++,
and you have bad C++ skills, improve them. It's better to take
the time to learn C++ correctly than to use a less appropriate