Mail Archives: djgpp/1998/06/18/05:52:33
On Mon, 15 Jun 1998, Pawel Kowalski wrote:
> Are there any differences in the question of speed
> between executables of the same source codes (consisting
> of only basic C functions) compiled in C and C++ by gcc?
>
> (I used to think that C++ is slower for it's objective
> language, and I don't know whether it's true or not...)
[ The following is based on *optimized* PGCC output ]
A C-only source file compiled as a C++ file (extension *.cc) will produce
almost identical assembler output as the same file compiled with *.c
extension. The main difference in the assembler files is that C++ declares
function names differently (i.e. function foo(int i) would be _foo in C
and _foo__FP1i (or some such name) in C++; the C++ compiler also tends to
insert many more labels (not all of which are used) in the code. Despite
that, the C++ assembler code is often (slightly) more compact.
Differences also lie in space reserved in the C++ version for virtual
function pointers etc, outside the function code.
There is a difference however between object-oriented C++ code and C code.
There are differences between macro's and templates, and macro's and
inline functions, and the effects of operator overloading on classes. In
theory, for instance, using operator overloading,
something like a = b*c + d in C++ where a,b,c and d are matrices, should
be relatively inefficient, because a temporary instance of a matrix has to
be initialized to contain the results of b*c, and the default matrix
constructor will be called.
For class methods declared and defined in a header file gcc will (usually)
optimize such an expression, so that it winds up being as efficient as C
code generated with the usual ADD_MATRIX(a,b) macro approach. For class
methods defined *outside* visible header files it can lead to performance
hits in unexpected places, especially if the class has constructors that
do a lot of memory allocation, error checking etc. The moral of the story
is, be careful about using operator overloading in performance sensitive
code.
Also I have noticed that [P]GCC's evaluation of templates is less than
optimal; it is very conservative about filling in inline functions.
Class functions that are inlined in normal C++ code are called as
functions when that class is used as a template argument, for instance.
Hope that helps. (I am not a C++ guru, so feel free to check my
observations against your own experience).
Elliott Oti
kamer 104, tel (030-253) 2516 (RvG)
http://www.fys.ruu.nl/~oti
- Raw text -