C++ speed vs. Fortran

I have developed a code under c++ for some engineering calculations in fluid mechanics. I transfered the code in Fortran 77 and benchmark the speed of 2 executables. The unexpected result: Fortan exe goes up to 4 times faster that c++!!! It was a big surprice for me, but i take a look in the web and i found some guys states that Fortan speeds better than c++.
The c++ (console application) compiled under Microsoft and Borland compilers (the results is almost the same) with all speed optimazations available from each one compiler. Program runs under Pentium 4. Also i must say that program uses about 30 MB of memory for arrays (dynamic allocated). All calculations are performed with double precision. c++ programm is using a class where all the action takes place. I tyied and c style, without a class usage, but the result is the same.
Can any one explain me what's going one?

Comments

  • : I have developed a code under c++ for some engineering calculations in fluid mechanics. I transfered the code in Fortran 77 and benchmark the speed of 2 executables. The unexpected result: Fortan exe goes up to 4 times faster that c++!!! It was a big surprice for me, but i take a look in the web and i found some guys states that Fortan speeds better than c++.
    : The c++ (console application) compiled under Microsoft and Borland compilers (the results is almost the same) with all speed optimazations available from each one compiler. Program runs under Pentium 4. Also i must say that program uses about 30 MB of memory for arrays (dynamic allocated). All calculations are performed with double precision. c++ programm is using a class where all the action takes place. I tyied and c style, without a class usage, but the result is the same.
    : Can any one explain me what's going one?
    :

    When C++ is compiled it is turned into 'object code', an inline assembler code. It expands the code a good deal, especially if an operation is lengthy in C++, it'll be even more lengthy in the object code. For example:
    2*3 would look kind of like this in the object file: 2*2*2. Expanding a simple calculation.

    Its possible to view the object code, make sure your compiler is set so it will save the object file for your viewing.

    Its possible to cut down on the amount of object code that you have(thus easing up work that the computer must do) by shortening operations, and even declearing functions with the keyword inline.
    There are probably various other ways this can be done, which would speed up the execution of the program.

    Also, correct me if I am wrong, is FORTRAN an interpreted language? Or is it compiled?
  • : : I have developed a code under c++ for some engineering calculations in fluid mechanics. I transfered the code in Fortran 77 and benchmark the speed of 2 executables. The unexpected result: Fortan exe goes up to 4 times faster that c++!!! It was a big surprice for me, but i take a look in the web and i found some guys states that Fortan speeds better than c++.
    : : The c++ (console application) compiled under Microsoft and Borland compilers (the results is almost the same) with all speed optimazations available from each one compiler. Program runs under Pentium 4. Also i must say that program uses about 30 MB of memory for arrays (dynamic allocated). All calculations are performed with double precision. c++ programm is using a class where all the action takes place. I tyied and c style, without a class usage, but the result is the same.
    : : Can any one explain me what's going one?
    : :
    :
    : When C++ is compiled it is turned into 'object code', an inline assembler code. It expands the code a good deal, especially if an operation is lengthy in C++, it'll be even more lengthy in the object code. For example:
    : 2*3 would look kind of like this in the object file: 2*2*2. Expanding a simple calculation.
    :
    : Its possible to view the object code, make sure your compiler is set so it will save the object file for your viewing.
    :
    : Its possible to cut down on the amount of object code that you have(thus easing up work that the computer must do) by shortening operations, and even declearing functions with the keyword inline.
    : There are probably various other ways this can be done, which would speed up the execution of the program.
    :
    : Also, correct me if I am wrong, is FORTRAN an interpreted language? Or is it compiled?
    :

    2*3 will NOT expand to 2*2*2.

    As I recall Fortran is a contraction of "Formula translation". It was written specifically for mathamatical work. So It would seem natural that it will create more efficent object code than C or C++. It would be interesting to compare the relavent pieces of object to see why one is faster than the other. (sounds like a job for asmGuru!)

    C/C++ may add more overhead, but my guess is that with any complex computation, most of the processing time is spent in a relatively small block of code so while the rest of it may make the exe larger, it probably doesn't significantly affect the benchmark speed.

    If really needed it to run fast in C/C++, I think you could disasemble the Fortran object code and import the relevant pieces as inline asm. Inline asm is not something I have done, so I a weak on the details, but it might be a fun to try.

    David
  • : : : I have developed a code under c++ for some engineering calculations in fluid mechanics. I transfered the code in Fortran 77 and benchmark the speed of 2 executables. The unexpected result: Fortan exe goes up to 4 times faster that c++!!! It was a big surprice for me, but i take a look in the web and i found some guys states that Fortan speeds better than c++.
    : : : The c++ (console application) compiled under Microsoft and Borland compilers (the results is almost the same) with all speed optimazations available from each one compiler. Program runs under Pentium 4. Also i must say that program uses about 30 MB of memory for arrays (dynamic allocated). All calculations are performed with double precision. c++ programm is using a class where all the action takes place. I tyied and c style, without a class usage, but the result is the same.
    : : : Can any one explain me what's going one?
    : : :
    : :
    : : When C++ is compiled it is turned into 'object code', an inline assembler code. It expands the code a good deal, especially if an operation is lengthy in C++, it'll be even more lengthy in the object code. For example:
    : : 2*3 would look kind of like this in the object file: 2*2*2. Expanding a simple calculation.
    : :
    : : Its possible to view the object code, make sure your compiler is set so it will save the object file for your viewing.
    : :
    : : Its possible to cut down on the amount of object code that you have(thus easing up work that the computer must do) by shortening operations, and even declearing functions with the keyword inline.
    : : There are probably various other ways this can be done, which would speed up the execution of the program.
    : :
    : : Also, correct me if I am wrong, is FORTRAN an interpreted language? Or is it compiled?
    : :
    :
    : 2*3 will NOT expand to 2*2*2.
    :
    : As I recall Fortran is a contraction of "Formula translation". It was written specifically for mathamatical work. So It would seem natural that it will create more efficent object code than C or C++. It would be interesting to compare the relavent pieces of object to see why one is faster than the other. (sounds like a job for asmGuru!)
    :
    : C/C++ may add more overhead, but my guess is that with any complex computation, most of the processing time is spent in a relatively small block of code so while the rest of it may make the exe larger, it probably doesn't significantly affect the benchmark speed.
    :
    : If really needed it to run fast in C/C++, I think you could disasemble the Fortran object code and import the relevant pieces as inline asm. Inline asm is not something I have done, so I a weak on the details, but it might be a fun to try.
    :
    : David
    :
    [blue]Also, lets not forget that you can build EXE with the debug info and without it - make sure your C++ compiler builds the RELEASE version of the code. I had a case in my career where the statistical analysis program was faster about 5 times (!) when in RELEASE mode. Because code built differently and optimizations are not switched ON.[/blue]
  • First thanks for your time.
    Fortran uses compiler and not interprenter as basic or java. Is true that the name comes from FORmulation TRANsoframion and developed especially for scientific calculations. Your remark about objects is something that i didn't take count. I also had the idea of inline assembly, but i think is to difficult for me to write...
    Searching the web i found that Intel has some math libryries targeting scientific calculations in C++. May be a have to try it also.
    If there are other ideas please inform me.
  • : First thanks for your time.
    : Fortran uses compiler and not interprenter as basic or java. Is true that the name comes from FORmulation TRANsoframion and developed especially for scientific calculations. Your remark about objects is something that i didn't take count. I also had the idea of inline assembly, but i think is to difficult for me to write...
    : Searching the web i found that Intel has some math libryries targeting scientific calculations in C++. May be a have to try it also.
    : If there are other ideas please inform me.
    :

    you might also try your experiment with Intell's c++ compiler because I understand it optimizes code better than other compilers for the Intell CPU chips.




  • : First thanks for your time.
    : Fortran uses compiler and not interprenter as basic or java. Is true that the name comes from FORmulation TRANsoframion and developed especially for scientific calculations. Your remark about objects is something that i didn't take count. I also had the idea of inline assembly, but i think is to difficult for me to write...
    : Searching the web i found that Intel has some math libryries targeting scientific calculations in C++. May be a have to try it also.
    : If there are other ideas please inform me.
    :

    I took Fortran in college, but since then have not had occasion to use it. It was indeed developed specifically for science apps, but it is odd to me that you would see such a significant difference in benchmark speed, given the fact that the calculations you refer to are most likely run on the same exact math co-processing circuits.

    Can you locate the .mak file used to compile the C code and post it here? I suspect there is a fundamental difference in the way the compilers are directed to generate the code. Is it possible that the Fortran compiler generates code that blocks multi-tasking?
  • :
    : you might also try your experiment with Intell's c++ compiler because I understand it optimizes code better than other compilers for the Intell CPU chips.
    :
    :
    :
    :
    :


    Using the Intel C++ compiler we get a massive 30% increase in FPS for graffics in a game engine when compared to the standard MS VC compiler.



    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : Using the Intel C++ compiler we get a massive 30% increase in FPS for graffics in a game engine when compared to the standard MS VC compiler.

    That's a rather bizarre blanket statement to make, considering that most games are GPU bound not CPU bound, and no amount of compiler-level optimization can help that.

  • FORTRAN has long spanked C++ for scientific computing. But it is possible to beat it if you know what you're doing. Dr. Dobb's Journal published an article a year or two ago about how to beat FORTRAN with C++ for numerical calculations, you might want to look it up. Google is your friend.

    And no, contrary to most of the replies you are getting, compiler optimizations aren't going to make that much of a difference.

  • : : Using the Intel C++ compiler we get a massive 30% increase in FPS for graffics in a game engine when compared to the standard MS VC compiler.
    :
    : That's a rather bizarre blanket statement to make, considering that most games are GPU bound not CPU bound, and no amount of compiler-level optimization can help that.
    :
    :

    You're right. Just relaying what I know through testing on a vector based D3D game. An optimizing compiler designed for a specific architecture makes a noticable difference. Theres's a free demo of the compiler somewhere on the Intel site if you want to try it out. Or you can follow the links and read why its better http://www.intel.com/software/products/compilers/cwin/
    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : Or you can follow the links and read why its better
    : http://www.intel.com/software/products/compilers/cwin/

    I believe that. Have you ever seen their profiler?
    It's freakin' alien technology.

  • : : Or you can follow the links and read why its better
    : : http://www.intel.com/software/products/compilers/cwin/
    :
    : I believe that. Have you ever seen their profiler?
    : It's freakin' alien technology.
    :
    :

    You mean VTune? It's unbelievable the data you can collect and process about your program and the system while it runs. Intel makes some of the best tools available in my opinion.
    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories