Subject: C++ vs Fortran performance

From: Richard Gillilan <[email-masked]>
Date: Tue, 28 Sep 1999 09:57:36 -0400

> Also, if speed is what is sought-for here--optimized C is just as fast as
> fortran.

I write all my code in C++ nowadays, but the most frequently asked
question I get
is "isn't C++ much slower than Fortran?". I have a paper on NAMD by
the Schulten group that presents benchmarks comparing their optimized 
C++ code with similar Fortran codes(*). On a single processor their code
takes about 20-30% longer (304 min vs 237-255 min). 
I have not been able to compare my own code with similar commercial
versions yet, but I think this is a very acceptable hit considering
all that is gained from C++ and OOP. 

Has anyone had a similar experience?

The paper also gives a few guidelines: 

         o use ordinary C for heavy numerical tasks (simple constructs)
         o use in-lining for critical functions
         o avoid virtual functions (runtime function lookups)

I have found these to be VERY important.
If you have additional guidlines, I would love to collect and summarize
for the list. 

(*) International Journal of Supercomputer Applications and High
Performance
    Computing Vol 10, No 4 Winter 1996 pp251-268

---

From: David van der Spoel <[email-masked]>
Date: Tue, 28 Sep 1999 22:49:12 +0200 (CEST)

>takes about 20-30% longer (304 min vs 237-255 min). 
This is a recurring topic.

The answer depends on application and compiler and can thus not be
answered unequivocally. The GROMACS MD code (http://md.chem.rug.nl/~gmx)
which was developed by me and others in the Berendsen group, contains code
in fortran and C that is identical. Yet the fortran version is always
faster, except when using g77 on Linux/x86 (and other platforms probably).
This was tested on SGI, DEC alpha, IBM SP2 and on Linux using the Portland
compilers. The raw performance of identical code written in different
languages is: f77 >= C >= C++.
If you are able to concentrate the number crunching in a small fraction
of the code, it may be worthwhile to go for mixed languages, like we did
with GROMACS. It is 99% C, and 1% fortran, combining the fortran speed and
the ease of development of C. 

Groeten, David.
________________________________________________________________________
Dr. David van der Spoel		Biomedical center, Dept. of Biochemistry
s-mail:	Husargatan 3, Box 576,  75123 Uppsala, Sweden
e-mail: [email-masked]	www: http://zorn.bmc.uu.se/~spoel
phone:	46 18 471 4205		fax: 46 18 511 755
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

---

From: Matt Challacombe <[email-masked]>
Date: Tue, 28 Sep 1999 10:08:46 -0600

Optimization in C (and C++) is NP hard.  Optimization in Fortran
is polynomial.  Thus, the difference between Fortran and C and
their various dialects becomes more apparent as code complexity
and compile time increases.  

-Matt

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ Matt Challacombe, Ph.D.           http://www.t12.lanl.gov/~mchalla/ +
+ Los Alamos National Laboratory    email: [email-masked]       + 
+ Theoretical Division              vmail:   (505) 698-4112           +
+ Group T-12, Mail Stop B268        phone:   (505) 665-5905           +
+ Los Alamos, New Mexico  87545     fax:     (505) 665-3909           +
+                                                                     +
+ "The secret to mountain biking is pretty simple. The slower you go  +
+  the more likely it is you'll crash." -- Julie Furtado              +
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

---

From: Jochen <[email-masked]>
Date: Tue, 28 Sep 1999 11:30:50 +0200

On Mon, 27 Sep 1999 Matt Challacombe wrote:
>On Mon, 27 Sep 1999, Iraj Daizadeh wrote:
>
>> Why in the world would someone want to perform this absurd transformation?
>
>One reason for performing this transformation is to ensure portability 
>for fortran-c and c-fortran interfaces where strings are passed.  

As said before, cfortran does the magic :-)
IIRC, it even works for strings.

-- Jochen
        Heinrich-Heine-Universität, Institut für Physikalische Chemie I
        Universitätsstr. 1, Geb. 26.43.02.29, 40225 Düsseldorf, Germany
phone 02118113681 fax 02118115195  --  www-public.rz.uni-duesseldorf.de/~jochen
[email-masked] -- [email-masked] -- [email-masked]

---

From: Andrew Horsfield <[email-masked]>
Date: Tue, 28 Sep 1999 15:57:08 +0100 (BST)

On Tue, 28 Sep 1999, Richard Gillilan wrote:

> question I get is "isn't C++ much slower than Fortran?". I have a
> paper on NAMD by the Schulten group that presents benchmarks comparing
> their optimized C++ code with similar Fortran codes(*). On a single
> processor their code takes about 20-30% longer (304 min vs 237-255
> min).  I have not been able to compare my own code with similar
> commercial versions yet, but I think this is a very acceptable hit
> considering all that is gained from C++ and OOP.
> 

I write all my code in C, and am not aware of any performance bottlenecks
introduced by the language. (There is one possible problem associated the
fact that a C compiler does not know if two parameters passed to a
subroutine share the same piece of memory or not - but even this has been
overcome by the new standard). Much more important, I suspect, is the
quality of the compiler, and the design of the code. I have found gcc
delivers good performance on x86 platforms. Problem that can severely
reduce performance (such as cache misses) are common to all languages.

If anyone has any experience with Ada, I would be interested to know how
this performs.

Best regards,

Andrew

  +----------------------------------------------------+
   Andrew Horsfield       e-mail: [email-masked] 
     FECIT, 2 Longwalk Road, Stockley Park, Uxbridge,   
          Middlesex UB11 1AB, United Kingdom.           
   phone: +44-(0)181-606-4653  FAX: +44-(0)181-606-4422 
  +----------------------------------------------------+

---

From: Oliver Kohlbacher <[email-masked]>
Date: Tue, 28 Sep 1999 17:47:01 +0200

> all that is gained from C++ and OOP. 
According to my experiences 30% seems to be an average value if you take a bit 
of care. Thoroughly optimized code can even be faster.
There is a paper by Veldhuizen and Jernigan ("Will C++ be faster than 
fortran", ISCOPE'97, extended abstract) that states that C++ might even be 
faster.
I can only recommend this paper and the stuff related to their Blitz++ library.

>          o avoid virtual functions (runtime function lookups)
I would add the following rules:
    o  use templates and template specialization. Although template code
       takes a long time to compile, it is very fast
    o  virtual function can often be replaced using templates
    o  use compact data structures to increase the locality of your code.
       Data encapsulation can also increases locality.
    o  use the better algorithms and data structures
	- C++ is much better suited to implement
       more sophisticated algorithms and data structures than FORTRAN and many 
       algorithms are already available in the libraries

Regards,
  Oliver
----
 Oliver Kohlbacher   ([email-masked])
 Max-Planck-Institut fuer Informatik, Im Stadtwald, 66123 Saarbruecken/GERMANY
 Tel.: 0681-9325-505 Fax: 0681-9325-199

---

From: [email-masked]
Date: Tue, 28 Sep 1999 18:51:05 -0400 (EDT)

>        algorithms are already available in the libraries

Also pretty important for C++ optimization:
    o  use references (the "&" operator) in parameter lists
    o  make liberal use of the "const" keyword, as in:
	    const memberFunction(const Classname &class, int i) const { ... }

--grg