views:

180

answers:

5

I'm currently using MSVC for C++ but as I'm switching to C to write a very performance-intensive program (interpreter) I have to search for a fitting C compiler.

I've looked at some binaries produced by Turbo-C and even if its old they seem pretty straigthforward and optimized.

Now I don't know what the best compiler for building an interpreter is, but maybe you can help me. I've considered GCC but as I don't know much about it, I can't be really sure.

+6  A: 

99.9% a programs performance depends on the code you write and the language you choose. you can safely ignore the performance of the the compiler.

Stick to MSVC...and dont waste time :)

SysAdmin
Again, why a compiler like MSVC which is optimized for C++ and not C??
samuel
samuel -- I'd be curious if you could cite a source for that comment.
WhirlWind
I dont think a c++ compiler like MSVC or GCC will produce "extra code"(which affects performance) just because the code is in C.
SysAdmin
Nope and as I said below, you can ask MSVC to compile for C specifically with the /TC option.
Ninefingers
@samuel - when you say optimize it is not your C/C++ code which is optimized. it is the asm instructions which are optimized. so you cannot say MSVC is optimized for C++.
SysAdmin
@SysAdmin: Modern compilers (even pretty old ones) are more complex than that. There's one, two, sometimes three intermediate languages (ILs) for abstract code in there. (say high, medium, low) There are certain optimizations done at each level, so it is very likely that some optimizations apply to C++ only. Based on this, you could easily imagine different compiler front-ends for the same compiler back-end (say C++ -> HIL.c++ -> MIL and C -> HIL.c -> MIL) having quite different levels of effort put in for optimizations. I have next to no knowledge of what's really going on in MSVC, though.
darron
The place where you would be most likely to see a C++ compiler produce worse code than a C compiler when compiling C would be in not taking advantage of assumptions that C compilers can make. I don't know that this is the case, but have never investigated it heavily. That being said, I doubt any loss would be that great.
nategoose
+1  A: 

If you want a lightweight program, it is not the compiler you need to worry about so much as the code you write and the libraries you use. Most compilers will produce similar results from the same source code.

For example, using C++ with MFC, a basic windows application used to start off at about 900kB and grow rapidly. Linking with the dynamic MFC dlls would get you down to a few hundred kB. But by dropping MFC totally - using Win32 APIs directly - and using a minimal C runtime it was relatively easy to implement the same thing in an .exe of about 25kB or less (IIRC - it's been a long time since I did this).

So ditch the libraries and get back to proper low level C (or even C++ if you don't use too many "clever" features), and you can easily write very compact applications.

edit

I've just realised I was confused by the question title into talking about lightweight applications as opposed to concentrating on performance, which appears to be the real thrust of the question. If you want performance, then there is no specific need to use C, or move to a painful development environment - just write good, high performance code. Fundamentally this is about using the correct designs and algorithms and then profiling and optimising the resulting code to eliminate bottlenecks and inefficiencies. Note that these days you may achieve a much bugger bang for your buck by switching to a multithreaded approach than just concentrating on raw code optimisation - make sure you utilise the hardware well.

Jason Williams
linked libraries and executable size are probably also a *very* minor factor in program performance.
Joey
Yeah, code size is not much of an issue... but too many abstract layers IS. Direct C switch statement style windows handling only what you need is MUCH faster than MFC, but it's pretty horrible to work with. Anyway, GUI or windows handling is not going to be in the critical path of a high performance interpreter... his stated goal.
darron
+4  A: 

If I were you, I would take the approach of worrying less about the compiler and worrying more about your own code. Write the code for the interpreter in a reasonable way. Then, profile it, and optimize spots based on how much time they take. That is more likely to produce a performance benefit than using a particular compiler.

WhirlWind
I'm searching for a fitting C compiler and MSVC is for C++
samuel
samuel -- I'd be curious if you could cite a source for that comment.
WhirlWind
It does C as well; you can change it using the VS property pages or by passing the /TC (I think) argument.
Ninefingers
@Ninefingers: That's unnecessary: *By default, CL assumes that files with the .c extension are C source files and files with the .cpp or the .cxx extension are C++ source files.* [/Tc, /Tp, /TC, /TP (Specify Source File Type)](http://msdn.microsoft.com/en-us/library/032xwy55%28VS.100%29.aspx) So all you really need to do is give the files *.c* extension (which is quite reasonable by itself) and you got it.
conio
@conio Aaaah Excellent, like gcc then really. I usually use CL via the vs pages and so change it there.
Ninefingers
A: 

You can use GCC, through MingW, Eclipse CDT, or one of the other Windows ports. You can optimize for executable size, speed of resulting executable, or speed of compilation.

Matthew Flaschen
I've noticed QtCreator for Windows as distributed is compiled with the MSVC compiler, but the included compiler for your use is MinGW. I've used both for the same Qt app doing heavy floating point math... and MinGW seems noticeably slower. Sorry, but I haven't actually benchmarked it or anything. MSVC has a much better debugger, too. I still prefer working on QtCreator in Linux and moving to Windows for test builds, though.
darron
A: 

C++ was designed to be backward compatible with C. So any C++ compiler should be able to compile pure C. You might want to tell it that it's C and not C++, so compiler doesn't do name mangling, etc. If the compiler is very good with C++, it should be equally good, or better with C, because C is much simpler.

I would suggest sticking with MSVC. It's a very decent system. Though if you are not convinced - compare your options. Build the same program with multiple compilers, look at the assembly they produce, measure the resulting executable performance, etc.

Nikolai N Fetissov
C++ clearly does not include all C99 features, and Microsoft's C++ compiler doesn't support C99 either.
Matthew Flaschen
@Matthew: When most people say "C" they mean C89/C90.
conio
`So any C++ compiler should be able to compile pure C.` this is wrong.. A lot of C code won't compile as C++, such as `int class;` or `int *abc = malloc(sizeof(int));`.
Andreas Bonini
OK, there are minor syntactic issues, so *legacy* C code won't always compile as C++. You are nitpicking. The answer is about C++ compilers being more or less supersets of C compilers, so the *optimization* features should work for both languages.
Nikolai N Fetissov
Nikolai, modern C code won't (all) compile as C++ either, since C99 is in fact C.
Matthew Flaschen
@Matthew: I'm well aware of that. The point I was trying to make was about C/C++ lineage and their compilers being *very* close relatives. I guess that didn't come out right :)
Nikolai N Fetissov