home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Reasons for using C vs. Fortran or vice/versa
- Message-ID: <1992Nov13.171542.10650@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Nov12.135901.15191@ccd.harris.com> <1992Nov12.210151.5486@alchemy.chem.utoronto.ca>
- Distribution: usa
- Date: Fri, 13 Nov 1992 17:15:42 GMT
- Lines: 51
-
- mroussel@alchemy.chem.utoronto.ca (Marc Roussel) writes:
-
- > It is easier to write Fortran that a compiler will properly optimize
- >than it is to write optimizable C. One chap at HP has a trivial routine
- >that the Fortran optimizing compiler immediately recognizes and does the
- >right thing with, but which needs to be heavily modified to obtain good
- >performance in its C version.
-
- This is a trifle ingenuous, or perhaps just incomplete.
-
- Historically, Fortran was an unstructured language. To get reasonable
- performance, compilers did extensive flow analysis and performed
- extensive optimization. Fortran was run on big machines, and
- compilers could take the time and space to do the analysis. This
- tradition of optimization has been carried forward to the present.
-
- Historically, C was a structured language developed on and for small
- machines. Flow analysis was less necessary, and typical machines
- were not suitable for large and complex compilers. Further, the
- nature of C and C programming styles made optimization less of a
- win (see below). In recent years, C compilers have begun to do more
- optimization.
-
- There are, however, language features which lead to fewer opportunities
- for optimization in C than in Fortran. Fortran doesn't use pointers
- the way C does. Fortran arrays, for example, are first-class objects,
- not converted to pointers as in C. If a C routine uses pointers, the
- compiler typically has no way of knowing what any of them point to.
- It cannot make simplifying assumptions as in Fortran, since any time
- a value is stored through a pointer, the compiler must assume that
- almost ANY data value might have been changed. Example:
- extern double x, y, z;
- void foo(double p[], double q[], int i)
- {
- p[i] = x + y;
- z = x + y + q[i];
- }
- Inside foo, p and q might point to the same object, and might point
- to x, y, or z. The first line might have changed the value of x or y,
- for example, so the compiler must reload the values of x and y for the
- second line, rather than using the remembered sum. This is the
- "aliasing" problem, and dramatically affects the efficiency of
- mathematical code in C compared to Fortran.
-
- With the equivalent Fortran code, the subroutine would have array
- parameters, and the compiler could assume that the arrays were
- disjoint from the global variables x, y, and z.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-