Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Speed of Fortran vs C

barbaraonli
Beginner
1,900 Views
We have converted an existing Fortran program, which does many iterations of calculations, to C and found that the Fortran runs about 4 times slower than the C version. The results of the two programs match exactly. I expected the Fortran version to be faster than the C version so I must be doing something wrong. The Fortran version is strictly Fortran 77 code compiled with the Intel compiler and the C version is compiled with the Microsoft C compiler and bothwerecreated in Visual Studio 2005. I did the timing tests by running the "release" versions of both programs, not the "debug" versions, outside the IDE from a BAT file. Any ideas? During the Fortran compilation I get messages like "Loop was vectorized", "Permuted loop was vectorized" and "partial loop was vectorized". For example, I get the message twice for the following piece of code which is not nested in any other loops (these are all single dimension COMMON integer arrays and MAXART is a COMMON integer):
[cpp]      DO 5 I = 1, MAXART
         ARTCL(I) = 0
         ARTRK(I) = 0
         ARTPR(I) = 0
         ARTCLS(I) = 0
         NUMSIG(I) = 0
         IF (I .LE. 10) YNSAT(I) = .FALSE.
    5 CONTINUE
[/cpp]
Do these messagesadversely affect the speed? The Fortran timing was calculated by calling CPU_TIME at the beginning and end of execution.
0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,900 Views
That you got those messages suggests to me that you have nested loops going through an array in the "wrong order" for Fortran, causing memory accesses to skip around. C stores arrays in "row-major order", where the rightmost subscript varies the fastest, but Fortran uses "column major order" where the leftmost subscript varies the fastest. If you directly translated the code and didn't reverse the order of the subscripts, you'll hurt performance. Evidently, the compiler tried to help you in some places, but perhaps was not able to do it everywhere.
0 Kudos
gib
New Contributor II
1,900 Views
That you got those messages suggests to me that you have nested loops going through an array in the "wrong order" for Fortran, causing memory accesses to skip around. C stores arrays in "row-major order", where the rightmost subscript varies the fastest, but Fortran uses "column major order" where the leftmost subscript varies the fastest. If you directly translated the code and didn't reverse the order of the subscripts, you'll hurt performance. Evidently, the compiler tried to help you in some places, but perhaps was not able to do it everywhere.
His conversion was from Fortran to C, so the C compiler has helped the performance. Maybe the Fortran code as written traverses arrays in row-major order.
0 Kudos
Reply