Thread: Performance on par with Fortran 90/95

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Performance on par with Fortran 90/95

    Hi everyone,

    I have a couple of questions. I was a C/C++ programmer in the past and I used it for number crunching. After a friend of mine told me about Fortran 90/95 and how it outperforms C/C++ for number crunching, I switched to Fortran 90. He was right, I was able to get about 50% performance boost using Fortran compared to C/C++.

    Due to the elegance and capabilities of C++, I kinda want to switch back but performance penalty is getting me worried.

    Is there any way we can get number crunching performance on par with Fortran 90/95 on C++? I checked out Blitz++ libraries, they look nice but it looks like the development has stopped. Are there any performance libraries like Blitz++ but more modern?

    I think the reason Fortran outperforms c++ is due to implicit vector/matrix operations and the ease of indexing on Fortran. Element-wise operations in C++ makes it not only run slower but also causes code to look less neat.

    For example to take the exponential of an array, in C++ you have to do it element by element whereas in Fortran does it in one line, and the compiler makes use of that.

    Any input would be appreciated.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Maybe this is your answer: Is Fortran faster than C? - Stack Overflow

    I don't think you should be seeing a 50% difference between optimized versions of a C program and a FORTRAN program. Maybe one algorithm works better in fortran than it does in C, or maybe fortran lends itself to the use of better number crunching techniques, but a good C programmer and a good C compiler can produce close-to-hand-assembly speed results.

    EDIT: I italicized the word close because I know someone out there is going to yell at me for saying that compilers are as good as the average assembly programmer.
    Consider this post signed

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And if you're doing vector/matrix operations, you can link to LAPACK from C just as well as you can from Fortran anyway. (I assume that's where you're getting "one line for exponential of an array", since I'm pretty sure that's not actually in Fortran itself.)

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bernt View Post
    I don't think you should be seeing a 50% difference between optimized versions of a C program and a FORTRAN program.
    I've seen much better than that with professionally written C code compared with professionally written Fortran code.
    Quote Originally Posted by bernt View Post
    Maybe one algorithm works better in fortran than it does in C, or maybe fortran lends itself to the use of better number crunching techniques, but a good C programmer and a good C compiler can produce close-to-hand-assembly speed results.
    While that is broadly true, it is also true that Fortran has a number of characteristics that optimise performance in some circumstances that are simply not possible in C. So a middling-to-good Fortran programmer can often get better performance than a highly skilled C programmer, in particular areas that Fortran is designed for.

    The Fortran programming model is, in several ways, simpler than the C programming model (eg very limited aliasing, simpler rules for type conversion). That means that an optimising Fortran compiler has opportunities to squeeze performance out of code that are simply not available to an optimising C compiler. Fortran compilers existed before C was even though of, so the optimisation phases of Fortran compilers are much more mature. Vendors also put a lot of effort into tuning Fortran optimisers to ensure they could extract the last bit of performance.

    In practice, a lot of quality numerical algorithms play very effectively to the strengths of Fortran (because that is what Fortran was designed for) and the same algorithms do not shine so well when converted to C - even with careful adjustments so they better exploit features of C.

    That's not saying C is inferior - just that is designed for things that Fortran is not. It is very difficult to implement something like a linked list in Fortran 77, for example.
    Last edited by grumpy; 04-15-2010 at 04:45 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You need to look into the `restrict' keyword, the variants of `restrict' support for C++, and compiler options to optimize under the assumption that the source requires no alias protection.

    Blitz++ development hasn't stopped. They just haven't updated the packages in a while. You can grab the latest source from CVS. As far as it goes, you aren't likely to get a "more modern" library for number crunching in C++.

    I've seen much better than that with professionally written C code compared with professionally written Fortran code.
    O_o

    You're saying that it would take over fifteen minutes for professionally sourced C++ compared to a ten minute benchmark for professionally sourced Fortran? The biggest difference I've ever seen is a few seconds.

    What are you calling professional exactly? I can see this difference for the obvious C++ versus the obvious Fortran. (With the obvious C++ versus professional Fortran I could even see the C++ taking ten times as long.)

    I'd dearly love to see this code.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. real-world apps in C++ (preferably simple)?
    By Aisthesis in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2009, 01:03 PM
  2. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  3. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 04:18 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Linking Fortran procedures with C program
    By gest in forum C Programming
    Replies: 5
    Last Post: 07-14-2003, 12:35 PM