Why are C++ based systems slower than C based systems?

Why are C++ based systems slower than C based systems?

In this section I will try to answer one of the very fundamental problem of professionals who are avid practitioners of C as a programming language and loathe transitioning to C++.

I have often come across software professionals who say "C++ based systems" are slower as compared to C based systems.

In fact if you ask any software engineer who has been using structured programming like C for more than 5-6 years to make a transition to C++ , his first line of defense will be the same …"Why do we have to transition to C++ when C++ based systems are slower as compared to C based systems?"

Do all the readers of this article agree that C++ based systems are slower as compared to C based systems?

I am sure most of you will agree that C++ is nothing but syntactic extension of C as a programming language to support object orientation.

If this is true then how is it possible C++ based systems are slower as compared to C based systems although the former is just a syntactic extension of the latter to support object orientation?

Let us understand … Even I agree to the fact that C++ based systems are slower as compared to C based systems , but it is not because of the language (C++) that the performance goes down , the performance goes down because of our design decisions to incorporate levels of indirection [supported by object orientation] within the C++ based systems so as to increase the usability and flexibility of a system. I can write a program or a system in C++ which can nearly give me the same level of performance like a C based system without using any level of indirection, in this case I am designing a system for performance and not for flexibility.

Now ask yourself why are maximum telecom/embedded system companies who have been previously using C based systems have started making a transition to C++ based systems?

The reason being

Software systems are mostly automating some complexity of a business system in the real world … while one of the fundamental properties of a business system is "Business systems are dynamic and will always continue to change …As the real world business system changes continuously … the software system/ automation system automating the same should also have to continuously evolve to reflect those changes. Hence Flexibility becomes one of the very dominant characteristics of a system .. as flexibility is all about how easily changes can be managed within systems" . If a structured [non flexible ] programming language like C is used to automate a dynamic business system .. with every change in the business system …. making the software system adapt to these changes can be a very difficult task severely affecting the manageability and maintainability.

Because of the above mentioned reasons, Flexibility, Manageability and Maintainability have become very dominant aspects of a system and hence of the software companies are transitioning towards C++ or Object Oriented systems.

This doesn't mean that we don't have to bother about the Performance. User will never tolerate a slow performing system. This is where judiciousness of a human mind should come into picture. We know flexibility and performance are inversely proportional to each other …. it is not possible to address both at the same time. Hence the best thing to do is find out functionalities within system where loss of performance is just not acceptable and changes don't happen very often.. for these functionalities don't use level of indirections. For functionalities where both performance and flexibility is needed, the decision of how many level of indirections that need to be introduced should be taken by the design team depending on the level of flexibility and the performance expected.

Many a times I come across participants who ask me "Hemant how do we decide whether we are supposed to use C or C++ as a programming language?"

The answer is very simple …. if performance is absolutely critical with no need for flexibility ….Use C … while if performance and flexibility both are needed use C++ or any object oriented programming language.

 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in