www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/18/05:52:33

Newsgroups: comp.os.msdos.djgpp
From: Elliott Oti <oti AT phys DOT uu DOT nl>
Subject: Re: C vs C++
Sender: usenet AT fys DOT ruu DOT nl (News system Tijgertje)
Message-ID: <Pine.OSF.3.95.980617120739.17286B-100000@ruunat.phys.uu.nl>
In-Reply-To: <199806152230390650.001F6C6E@pogwizd.tcs.uni.wroc.pl>
Date: Wed, 17 Jun 1998 11:00:47 GMT
References: <199806152230390650 DOT 001F6C6E AT pogwizd DOT tcs DOT uni DOT wroc DOT pl>
Mime-Version: 1.0
Organization: Physics and Astronomy, University of Utrecht, The Netherlands
Lines: 51
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019