Generally, are the programs written by C++ slower than written
by C 1
0% ?
[...]
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.
I agree that tuning C code could easily mean rewriting large parts of the
program. Yet this can be done if the program is small. If it is practical
or not is another question.
a lot depends on how code is structured and organized as well...
[...]
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.
Passing a STL container to a function by a copy instead of a reference or
using a copy where swap() would work - these are easy mistakes to make
and would qualify as a change in the algorithm (O(1)->O(N)). If one is
doing this all over the place, the profiler might not even bring it out
in non-critical paths, meaning that all the code would be just somewhat
slower than necessary.
OTOH, in C it takes some extra effort to pass an array into a function by
copy, so nobody would do this accidentally.
yep.
depending as well on how things are structured, one can often change the
algorithms used internally without impacting its interface.
Only if one tries to emulate encapsulation by using means of non-zero
runtime cost, like dynamically allocated data structures accessed via
opaque pointers. If one does not attempt to have any encapsulation, then
there is nothing which would get in the way of having fast code. Of
course, ensuring that this fast code is also *correct* and *robust* and
*maintainable* is significantly harder in C, unless the project is really
small.
agreed.
however, it is worth nothing that C and C++ better lend themselves to,
and thus traditionally use, notably different ways of achieving abstraction.
one language is not the other.
C++ more traditionally uses structural abstractions (templates being a
major example, also classes), where basically one has "something" which
can be interacted with, but hides its internal workings from view.
these common structures are then often shared over large portions of the
codebase, and it is assumed that some level of commonality is achieved
by consistent use of the same structures and abstractions across much of
the program.
hence, code reuse and uniformity are somewhat emphasized in this style.
C more traditionally is abstracted regionally, more like a large number
of brick-walled cities. all sorts of things can happen within a given
set of walls, and what happens outside is "none of anyone's business"
(typically, at any particular location, the level of abstraction is
fairly low, with bit-twiddling and mucking around with pointers and
pointer-arithmetic being "the business of the day").
typically, at the high-level, abstraction is achieved by essentially
enforcing these walls, and limiting or prohibiting interactions between
disparate systems ("action at a distance").
typically, this is done at a much coarser level than in traditional OOP
(say, an individual component could be easily anywhere from 10 to 50 kloc).
even though similar algorithms/... may be used in different places, it
is usually against-policy to access them directly, and in-fact it is
more common to simply copy/paste/edit the code into the new location,
specifically as this will avoid creating a dependency between otherwise
unrelated components (any such dependency may limit the ability for one
part to be changed independent of the other, compromising flexibility).
so, typically components are only allowed to directly interact with
"adjacent" components, and usually then "through a brick wall" (a set of
public API functions).
often, OS facilities are fit into the model in roughly a similar manner,
so code in one place may be allowed to use certain OS facilities, and in
another place, doing so will be forbidden.
which data-types, API calls, ... will also depend very much on where the
code is located (every city has, to a large extent, its own local rules
and customs, which may be strictly enforced).
other cases, where a more fluid structuring is needed, typically one may
have APIs which simply serve to redirect somewhere else or to something
else (basically, APIs serving a role mostly to ease communication
between components while at the same time attempting to avoid
compromising borders).
typically the goal of such an API is to provide a common interface to
functionality exposed by several different components. however, its own
local conventions are unique to itself (and similar rules apply as
before, it itself may be subject to only being allowed in certain code
regions).
sometimes, the combinations of the above may lead to a certain amount of
indirection (FWIW, interface-routing APIs, namely exposing an API to
allow certain functionality in a region of code where otherwise it would
not have been allowed).
....
but, these differences may lead to very different views WRT things like
ideal system architecture and development practices.
I don't personally believe any of this to be matters of "good" vs "bad"
(or "competent" vs "incompetent", or "smart" vs "stupid") or anything of
the sort, rather, the ultimate higher goal is "what works best in a
given situation".
or such...