Search code examples
c++cperformance

Performance penalty compiling C with a C++ compiler?


Is there any performance penalty if one compiles C code with C++ compiler?

I have a C like code and use MinGW C++ compiler. I'm using qmake to compile the project. If there is a performance gain if I switch the compiler to compile in C, I'll have to update the code, there are some incompatibility with the syntax and want to know if it worth it.


Solution

  • There should be very little, if any, performance difference, to the point that if there is a difference, it will be very nearly immeasurably small - assuming you're using a C and C++ compiler from the same vendor or collection. Using a C compiler from one vendor and a C++ compiler from another will likely show larger differences, but only due to the fact that different vendors implement different optimization strategies.

    There are a small number of potential optimization opportunities that C++ calls for that C compilers may not natively support - but again, unless they're from different vendors, the difference will be meaningless, and many compilers implement similar optimizations in both C++ and C compiler frontends.

    One exception to this is Microsoft's compiler - Microsoft never made a C compiler, to my knowledge.

    Note: I am assuming that the code does not use C++-specific features like templates or classes.