Is C Faster Than C++ (Deep Dive)

Many people think that C is faster than C++. Is that really the case? In this article, we are going to demystify it. We are going to try to understand that from the compiler perspective, and show you the truth.

Background About C And C++

To better understand whether C is faster than C++, let’s try to build up some of background here.

What Is C? What Is Its Focus?

The earliest version of C was invented in 1972. At the time it was invented, people were not building super large scale softwares. So the focus was really to achieve a specific goal, e.g., automate something. You can think of automation as a procedure. The pholosophy of C is really to allow you to implement the procedure.

To focus on a procedure implementation, the most important thing is to support control flow, e.g., if condition branching, switch logic, loops. That being said, the most important part of C (or any other procedure programming language) is to allow you define control flows as you want.

It’s Easy To Get Messy

As time goes by, the needs for building larger scale software were growing. Although C allows you to implement these softwares, it is challenging, if your code is not logically well organized.

A program is almost always composed of many modules. It might not be surprising to see that software without careful design has their modules interact with each other like a full mesh (an example shown in the following figure), which is a total nightmare.

Why is this a nightmare? Because every time you want to update the code for a single module, you might need also update all of other modules as well to make it work, because all of the modules are tightly coupled with each other. It might sounds ok if you only have a few modules, but think about the case where you have 100+ or 1k+ modules. It will collapse for sure.

Object Oriented Programming

One of the most important reasons that large project can get messy is that people focus too much on procedure. In order to make it more organized, Object Oriented Programming (OOP) was proposed. OOP proposes to focus on a module centric solution. We should focus on building reusable modules, instead of only focusing on completing the procedure. With each module carefully designed, it is easy to put them together to achieve a bigger goal, i.e., the whole procedure.

With the OOP concept, it is a lot easier to build highly modular system design, which makes the overall system a lot easier to manage. For example, an arch looks like the following:

What Is The Difference Between C And C++?

A short answer is, C++ is a super set of c.

C++ langauge includes two major functionalities:

  • Control flow support (just like what C language provides)
  • OOP concept implementation (the C++ class, which does not exist in C)

Both C and C++ have the first part. The second part is the biggest difference between C and C++.

Is C Faster Than C++?

Coming back to the question, “is C language faster than C++”, short answer is C++ is not necessarily slower than C.

Next, let’s compare the performance for two different scenarios.

Scenario 1: Same Code With Simple Control Flows

As discussed in the previous section, C and C++ shares the same control flow support logic. If we were to write the same code here, and compile them using C and C++ separately. There is a good chance that they behave the same.

For example, I wrote the following code:

#include <stdio.h>

int add(int x, int y)
{
    return x + y;
}

int main()
{
    printf("%d\n", add(1, 2));
    return 0;
}

The above code can be compiled by both C and C++ compiler. So I used both gcc and g++ to compile it separately, and it turned out the assembly generated by both compiler are the same, which is

	.file	"main.cpp"
	.intel_syntax noprefix
	.text
	.globl	_Z3addii
	.type	_Z3addii, @function
_Z3addii:
	push	rbp
	mov	rbp, rsp
	mov	DWORD PTR -4[rbp], edi
	mov	DWORD PTR -8[rbp], esi
	mov	edx, DWORD PTR -4[rbp]
	mov	eax, DWORD PTR -8[rbp]
	add	eax, edx
	pop	rbp
	ret
	.size	_Z3addii, .-_Z3addii
	.section	.rodata
.LC0:
	.string	"%d\n"
	.text
	.globl	main
	.type	main, @function
main:
	push	rbp
	mov	rbp, rsp
	mov	esi, 2
	mov	edi, 1
	call	_Z3addii
	mov	esi, eax
	lea	rdi, .LC0[rip]
	mov	eax, 0
	call	printf@PLT
	mov	eax, 0
	pop	rbp
	ret
	.size	main, .-main
	.ident	"GCC: (Debian 8.3.0-6) 8.3.0"
	.section	.note.GNU-stack,"",@progbits

That means, for the above program, it has same performance no matter it is compiled by C or C++.

Scenario 2: Advanced Code Only C++ Support

If we were to compare the advanced features in C++ with basic features in C, there is a good chance that C++ is slower than C. This is not because C++ was not able to match C’s performance. The real reason is that to support advanced features, we need more computing power, which might make things slower.

For example, if we were to compare a C++ class virtual function call with a simple C function call. It is for sure that C++ is slower, because virtual function call will need one more CPU instruction to look up the vtable.

Should I Use C Or C++?

If you’d like to develop a program, C++ is almost always a better choice. The performance difference between C and C++ is not almost neglectable, and in theory, you can achieve the same performance no matter you are using C or C++.

Unless you are super picky about performance, and you’d like to try your best to avoid C++ features to prevent the performance hit, C++ is the way to go.