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

C function vs Fortran function performance

shachris23
New Contributor I
2,258 Views

Hi,

Not trying to start a language war or anything, but I work in a real-time image processing industry where performance is of utmost concern.

Our initial algorithms were written in Fortran back in the 70's/80's, and eventually translated to C. Right now our application uses C++ for infrastructure-related stuff, and C for the algorithms for maximum performance. The C algorithm functions are parallelized using master-worker design pattern. We use Intel compiler. Our platform is Windows x64.

I have never coded in Fortran but heard that it's great for scientific computing, as well as unmatched (?) performance by other language.

So...here's the question: assuming an algorithm is coded efficiently in C and Fortran (i.e. they both describe the same algorithm), would code produced in Fortran be significantly faster than the C counterpart? Assuming that we use Intel compiler for both C and Fortran.

Because if Fortran really is faster, I'm thinking about making an object code out of Fortran and call it from our C++ infrastructure instead.

Thanks!!

S.

0 Kudos
1 Solution
Steve_Nuchia
New Contributor I
2,258 Views

The main differences are

one compiler may simply be better, or better adapted to your particular situation, than the other.

the fortran library is likely to be better tuned for many mathematical operations -- but you can call it from C if you want.

the fortran language permits the compiler to make certain optimizations that are seldom possible in standard C / C++, mainly due to potential aliasing but also to potential recursion / reentrancy issues. It is usually possible to provide "hints" to a performance-oriented C / C++ compiler that will allow it to make the same optimizations.

In the end, in my opinion, it is easier to express and understand algorithms that involve more advanced data structures in C / C++ than in the fortran that I learned. Modern fortran may be better but it still feels very combersome to me. Since better algorithms beat better code every time, use the language that admits the most natural and most maintainable expression of the algorithm.

View solution in original post

0 Kudos
6 Replies
g_f_thomas
Beginner
2,258 Views
Quoting - shachris23

Hi,

Not trying to start a language war or anything, but I work in a real-time image processing industry where performance is of utmost concern.

Our initial algorithms were written in Fortran back in the 70's/80's, and eventually translated to C. Right now our application uses C++ for infrastructure-related stuff, and C for the algorithms for maximum performance. The C algorithm functions are parallelized using master-worker design pattern. We use Intel compiler. Our platform is Windows x64.

I have never coded in Fortran but heard that it's great for scientific computing, as well as unmatched (?) performance by other language.

So...here's the question: assuming an algorithm is coded efficiently in C and Fortran (i.e. they both describe the same algorithm), would code produced in Fortran be significantly faster than the C counterpart? Assuming that we use Intel compiler for both C and Fortran.

Because if Fortran really is faster, I'm thinking about making an object code out of Fortran and call it from our C++ infrastructure instead.

Thanks!!

S.

It's hard to tell a priori. Modern C (as distinct from C++) and Fortran are comparably fast and which you use can be a personal choice unless local policy dictates otherwise. I'm surrounded by Ccolleagues but Iprefer that all my bugs be of the Fortran type if only because I can detect and swat them more easily. Fortran is simple to learn and you can quickly recode a C algorithm and see which is faster (for that algorithm only and assuming both codes are equally well written). Did you know that Fortran 2003 is interoperable with C?

Gerry

0 Kudos
shachris23
New Contributor I
2,258 Views
Quoting - g.f.thomas

It's hard to tell a priori. Modern C (as distinct from C++) and Fortran are comparably fast and which you use can be a personal choice unless local policy dictates otherwise. I'm surrounded by Ccolleagues but Iprefer that all my bugs be of the Fortran type if only because I can detect and swat them more easily. Fortran is simple to learn and you can quickly recode a C algorithm and see which is faster (for that algorithm only and assuming both codes are equally well written). Did you know that Fortran 2003 is interoperable with C?

Gerry

Oh wow..I never knew modern Fortran is interoperable with C. Just now I also looked up about Fortran on Wikipedia and found that the language is going through a lot of change to support parallelism. How great!

Interesting. Thanks for your thoughts.

0 Kudos
Steve_Nuchia
New Contributor I
2,259 Views

The main differences are

one compiler may simply be better, or better adapted to your particular situation, than the other.

the fortran library is likely to be better tuned for many mathematical operations -- but you can call it from C if you want.

the fortran language permits the compiler to make certain optimizations that are seldom possible in standard C / C++, mainly due to potential aliasing but also to potential recursion / reentrancy issues. It is usually possible to provide "hints" to a performance-oriented C / C++ compiler that will allow it to make the same optimizations.

In the end, in my opinion, it is easier to express and understand algorithms that involve more advanced data structures in C / C++ than in the fortran that I learned. Modern fortran may be better but it still feels very combersome to me. Since better algorithms beat better code every time, use the language that admits the most natural and most maintainable expression of the algorithm.

0 Kudos
shachris23
New Contributor I
2,258 Views
Quoting - Steve Nuchia

The main differences are

one compiler may simply be better, or better adapted to your particular situation, than the other.

the fortran library is likely to be better tuned for many mathematical operations -- but you can call it from C if you want.

the fortran language permits the compiler to make certain optimizations that are seldom possible in standard C / C++, mainly due to potential aliasing but also to potential recursion / reentrancy issues. It is usually possible to provide "hints" to a performance-oriented C / C++ compiler that will allow it to make the same optimizations.

In the end, in my opinion, it is easier to express and understand algorithms that involve more advanced data structures in C / C++ than in the fortran that I learned. Modern fortran may be better but it still feels very combersome to me. Since better algorithms beat better code every time, use the language that admits the most natural and most maintainable expression of the algorithm.

I agree with your assertion there. I believe the language that can best solve the given problem should be used.

By the way, let's say if someone use Fortran with CC++ code, how does one go about debugging it? So if we called a Fortan code from C...how does one go about "stepping into" Fortran code if needed be?

My environment is Windows Visual Studio C++.

Thanks,

S.

0 Kudos
Steven_L_Intel1
Employee
2,258 Views
Quoting - shachris23

I agree with your assertion there. I believe the language that can best solve the given problem should be used.

By the way, let's say if someone use Fortran with CC++ code, how does one go about debugging it? So if we called a Fortan code from C...how does one go about "stepping into" Fortran code if needed be?

My environment is Windows Visual Studio C++.

Just do it. It works just as if you stepped into a C routine.

My philosophy on these language wars is that you should choose the language that makes the most sense for that aspect of the application, that you know well and that can be maintained by your organization. No matter how good Fortran is, it's a poor choice if it has to be maintained by people who know only C++. Every language has its strengths and weaknesses. Historically, C/C++ has been weak in array operations compared to Fortran. That's still largely true, but some of the performance issues can be remedied by use of new features such as accelerated valarray.

Modern Fortran is very expressive and does not lack any significant features compared to usable C++. I'll comment that there have been many studies of projects that settled on C++ - the studies found that a majority of these projects never got completed or finished late and had significant maintenance costs. This is more likely to happen when developers are given free reign of all the C+ features and mis-features. Fortran does not have this characteristic.

0 Kudos
Antonio_M
Beginner
2,258 Views
In my opinion, it all depends from the model you want to realize.
If your model can be just expressed in matrix terms, then nothing beats fortran efficency and expressivity.
In all other casesC++ is the best choice.
Just to give a more concrete example: if I want to model a soft computing technique like, for example, a neuro-fuzzy engine to process certain data, I would surely use C++. Would I need to realize a code for a fluid dynamics simulator using pseudospectral methods, I would surely use Fortran.
Best thing isthat, as already underlined, using F2003 ISO_C modules you have full interoperability between languages, so it's common for me to call C++ routines from Fortran programs and vice versa (just remember to declare C++ "entrance" points as "extern "C"" sinceISO_C is designed to manage interoperability withC) without sacrifying portability.

0 Kudos
Reply