0

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.

11
  • 7
    Why is it that "performance" ranks so high in the list of potential reasons for or against a particular approach? At least once per week I see a question "X is supposed to be bad, why? Performance?" or "I got these completely different approaches which might have different semantics and probably have quite different maintainability - which one is more performant?"... (Also, if you need to change to the code make it compile under a C compiler, it's not C code. Get your language usage straight.) Commented Sep 3, 2011 at 21:50
  • 1
    I mean by C code the fact that I'm not using OO/STL, only C runtime functions and winapi. The performance is very critical in my situation due to the fact that I'm hooking some of windows api. This is a security application i'm writing for my organization, so troll other's questions please. Commented Sep 3, 2011 at 21:56
  • 2
    I'm not trolling, I'm seriously astounded. If performance is critical, profile and profile and profile instead of asking very general (and hence hard to answer and not guaranteed to apply to your case at all) questions. Commented Sep 3, 2011 at 22:05
  • 2
    "This is a security application i'm writing for my organization" Which means, performance is absolutely, certainly not THE most important factor here (at least I really hope so). C++ isn't a superset of C, some C code may not compile with a C++ compiler and much worse: There are also small semantic differences of code which will compile but do different things (certainly not often a problem, but it still invites problems, bugs and security flaws) Commented Sep 3, 2011 at 22:05
  • 1
    @sbi: We get such questions for Java as well. Plus, Java really isn't the language you choose if you don't care about performance - then, dynamic languages are a very viable alternative :) Commented Sep 3, 2011 at 22:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

"Microsoft never made a C compiler". Well, I guess Windows isn't written in C then. And what do you think happens when you throw a file with .c extension to CL? Look at the /Tc option.
+1 To my knowledge, there is no penalty for unused features in C++. Therefore there is no performance difference. However, sometimes exception handling gets enabled which can hurt performance a bit. Microsoft does have a C compiler, but it's C90. They have no planes to support C99.
@David actually he's correct Microsoft's C compiler is just a ruleset for c89 slapped onto the c++ compiler that they have.
@David: Who said Microsoft use their own compiler for Windows? But yes, there should be at least some C-specific code in cl.exe (otherwise, code like Foo *foo = malloc(sizeof(*foo)) wouldn't compile as cleanly as the C standard requires because the C++ standard requires an error).
@greyfade Microsoft were shipping a 'C' compiler in the 1980s, years before they ever contemplated C++. This plus its lax typechecking is one reason for the dreaded Simonyi notation.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.