Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation and execution time of C++ vs C source code

I am not sure if this is a valid comparison or a valid statement but over the years I have heard folks claiming that the programs written in C++ generally take a longer time for compilation than the same written in C and that the applications coded in C++ are generally slower at run time than ones written in C.
Is there any truth in these statements?
Apart from reaping the benefits of OOP flexibility that C++ provides, should the above comparison be given a consideration purely from a compilation/execution time perspective?

I hope that this doesn't get closed as too generic or vague, it is just an attempt to know the actual facts about statements I have been hearing over the years from many programmers(C programmers predominantly).

like image 756
Alok Save Avatar asked Mar 24 '11 14:03

Alok Save


People also ask

What is difference between compile time and run time in C?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

Which one is faster in terms of execution time?

For a running program, the execution speed of Java can actually be faster that C since the runtime can make intelligent decisions based on the hardware it's running on. This is especially true of you are on multi-processor hardware and you are dealing with larger lists.


1 Answers

I'll answer one specific part of the question that's pretty objective. C++ code that uses templates is going to be slower to compile than C code. If you don't use templates (which you probably will if you use the standard library) it should be very similar compilation time.

EDIT: In terms of runtime it's much more subjective. Even though C may be a somewhat lower level language, C++ optimizers are getting really good, and C++ lends itself to more naturally representing real world concepts. If it's easier to represent your requirements in code (as I'd argue in C++) it's often easier to write better (and more performant) code than you would in another language. I don't think there's any objective data showing C or C++ being faster in all possible cases. I would actually suggest picking your language based on project needs and then write it in that language. If things are too slow, profile and proceed with normal performance improvement techniques.

like image 199
Mark B Avatar answered Oct 07 '22 17:10

Mark B