(That C++ is slower thing again) Re: the damned GUI - was:[PD] Pd in white on black and OSC

Marc Lavallée odradek at videotron.ca
Sun Nov 23 19:22:07 CET 2003

On Sun, Nov 23, 2003 at 05:57:09AM -0500, Larry Troxler wrote:

> I don't agree that for an equivalent program, it is slower than C. 
> I was actually optimistic that you would actually provide some examples 
> (I don't mind at all being proved wrong).

Maybe we should move this discussion to the dev list, but it might be 
interesting to some other people.

Here's a very simple exercise; it doesn't prove that C is necessarely
better than C++, but it shows that code compiled with g++ produces bigger
executable code than gcc, and that C++ code produces even bigger code than
C code compiled with g++.

A simple program in C (hello.c):
#include <stdio.h>
int main()
	puts( "Bonjour le monde!\n" );

The same program in C++ (hello.cpp):
#include <iostream>
int main()
	std::cout << "Bonjour le monde!\n";

Compilation of the C program with gcc:
$ gcc -o hello_c-gcc hello.c

Compilation of the C program with g++:
$ g++ -o hello_c-g++ hello.c

Compilation of the C++ program with g++:
$ g++ -o hello_cpp hello.cpp

Stripping the binaries:
$ strip --strip-unneeded hello_c-gcc hello_c-g++ hello_cpp

Size of the binaries:
$ ls -l hello_c-gcc hello_c-g++ hello_cpp
-rwxr-xr-x    1 marc     marc         2836 nov 23 12:35 hello_c-gcc
-rwxr-xr-x    1 marc     marc         2940 nov 23 12:35 hello_c-g++
-rwxr-xr-x    1 marc     marc         3840 nov 23 12:35 hello_cpp

Size of the machine code (disassembled with objdump):
$ objdump -d hello_c-gcc | wc
    222    1080    9827
$ objdump -d hello_c-g++ | wc
    222    1080    9827
$ objdump -d hello_cpp | wc
    268    1393   12053

Once dissambled, binaries of the C code compiled with gcc and g++
are exactly the same, so they probably run at the same speed.
The C++ code produces mode machine instructions than the C code, 
so it probably takes more time to run than its C equivalent.

I'm not an expert, and this exercice is probably bogus.

Googling around, I found this page about C vs C++ performance:
The analysis basically says that C++ add some overhead that can be reduced.


More information about the Pd-list mailing list