Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When I was studying in the university I often heard the idea that Fortran compilers produced faster code than C compilers for an equivalent program.

The key reasoning went like this: a Fortran compiler emits on average 1,1 processor instruction per line of code, while a C compiler emits on average 1,6 processor instruction per line of code - I don't remember the exact numbers but the idea was that C compilers emitted noticeably more machine code and therefore produced slower programs.

How valid is such comparison? Can we say that Fortran compilers produce faster programs than C compilers or vice versa and why does this difference exist?

share|improve this question
9  
That may simply mean that Fortran programs are more verbose than C... A meaningful comparison could only be done by implementing the same functionality in both languages and comparing the resulting machine code (size and speed). –  Péter Török Mar 21 '11 at 8:35
    
Also, does the generated code support parallel execution? –  user1249 Mar 21 '11 at 9:00
    
@Péter Török, it simply means that, say, BLAS and LAPACK in Fortran used to perform much better then any of their C/C++ ports. Now the gap is shrinking quickly. –  SK-logic Mar 21 '11 at 9:48
1  
You can only argue that one compiler produces faster code if you have a 100% equivalent program in both languages, written by experts who know their compilers and who can account for performance. –  Falcon Mar 21 '11 at 12:16
add comment

5 Answers

up vote 22 down vote accepted

IIRC one of the main reasons why Fortran is said to be faster is the absence of pointer aliasing, so they can use optimizations that C compilers can't use:

In FORTRAN, function arguments may not alias each other, and the compiler assumes they do not. This enables excellent optimization, and is one major reason for FORTRAN's reputation as a fast language. (Note that aliasing may still occur within a FORTRAN function. For instance, if A is an array and i and j are indices which happen to have the same value, then A[i] and A[j] are two different names for the same memory location. Fortunately, since the base array must have the same name, index analysis can be done to determine cases where A[i] and A[j] cannot alias.)

But I agree with others here: Comparing the average number of assembler instructions generated for a line of code is complete nonsense. For instance a modern x86 core can execute two instructions in parallel if they don't access the same registers. So you can (in theory) gain a performance increase of 100% for the same set on instructions just by reordering them. Good compilers will also often generate more assembly instructions to get faster code (think loop unrolling, inlining). The total number of assembler instructions says very little about the performance of a piece of code.

share|improve this answer
    
Another reason for better optimisations is the native support for complex numbers. –  SK-logic Mar 21 '11 at 10:52
    
Certainly correct for Fortran IV or so. Not sure if modern FORTRANs still don't have pointers, dynamic meory etc. –  Ingo Mar 21 '11 at 15:33
    
That's the same reason we often dropped down to a bit of inline assembly when developing in C and C++ in the games industry. People can claim as often as they like that "compilers can optimize better than humans writing assembly", fact is, pointer aliasing means that they often cannot. The code we can write by hand would be technically illegal for the compiler to emit, knowing as it does nothing about pointer aliasing. –  Carson63000 Mar 21 '11 at 19:45
    
C's restrict keyword allows the author of a function to specify that a pointer has no aliases. Is this sufficient to address the difference, or is there more to it? –  bk. Oct 21 '12 at 2:26
add comment

Completely invalid comparison.

First, as @Péter Török points out, you must first compare the number of lines in equivalent programs from Fortran and C for this to even be a valid comparison on the number of lines produced.

Second, less lines of code doesn't always equal faster programs. Not all machine instructions take the same number of cycles to execute, but you also have other issues such as memory access, caching, etc.

On top of that, long code runs can be faster since it results in a lower number of execute lines (ie, Line Count != Executed Line Count).

share|improve this answer
add comment

I think part of it is that FORTRAN compilers are designed to do some types of math very fast. Which is kind of why people use FORTRAN, to do calculations as fast as possible

share|improve this answer
add comment

Dan is correct, longer programs don't mean slower programs. It depends greatly one what they are doing.

I'm no expert on Fortran, I know a bit. Comparing them, I would think well written C would do much better in performance with more complex data structures and functionality than Fortran. Someone (please) correct me if I'm wrong here, but I do think Fortran is somewhat on a 'lower level' than C. If so, I'm sure for some problems would come out faster on Fortran.

Another thing, at first glance I thought you were asking if the compilers are faster. I actually do think that Fortran will generally compile faster for similar amounts of code, but the resulting program and how it runs would be a different story. It is just simpler to parse through.

share|improve this answer
1  
If you are using complex data structures FORTRAN is probably the wrong choice. FORTRAN is optimized to do simple number crunching very fast. –  Zachary K Mar 21 '11 at 13:05
add comment

The statement may have been true in the old days (circa late 70s) when C was in its infancy, and Fortran was supported by all major manufacturers and were highly optimized. Early Fortrans were based on the IBM architecture so simple stuff like the arithmetic if would certainly have been one statement per assembly instruction. This is true of the older machines like Data General and Prime, which had 3 way jumps. This doesn't work on modern instruction sets which do not have a 3 way jump.

Lines of code does not equal statements of code. Earlier versions of Fortran only allowed one statement per line. Later versions of Fortran can take multiple statements per line. C can have multiple statements per line. On the faster production compilers like Intel's IVF (formerly CVF, MS Powerstation) and Intel's C, there really is no difference between the two. These compilers are highly optimized.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.