LWN.net Logo

Russell: GCC and C vs C++ Speed, Measured

Rusty Russell ran an investigation to determine whether code compiled with the GCC C++ compiler is slower than code from the C compiler. "With this in mind, and Ian Taylor’s bold assertion that 'The C subset of C++ is as efficient as C', I wanted to test what had changed with some actual measurements. So I grabbed gcc 4.7.2 (the last release which could do this), and built it with C and C++ compilers." His conclusion is that the speed of the compiler is the same regardless of how it was built; using C++ does not slow things down.
(Log in to post comments)

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 18:37 UTC (Fri) by nix (subscriber, #2304) [Link]

Take *that*, abstraction penalty!

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 18:47 UTC (Fri) by daney (subscriber, #24551) [Link]

Except there was no abstraction other than that that is possible to achieve using the C language. So this test doesn't even try to measure any hypothetical Abstraction Penalty that may be incurred by using C++ features.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 19:10 UTC (Fri) by nix (subscriber, #2304) [Link]

I'm fairly sure (OK, certain) that actual C++ code has already landed in 4.8 (see e.g. gcc/hash_table.* on the 4.8 branch). That makes it a test of the abstraction penalty imposed on the optimizer by those classes (in effect, nil). (Admittedly, quite a lot of that is probably because of -fno-exceptions, which eliminates a huge bunch of abnormal edges that *would* otherwise impede optimization even of code that looks just like C code. I should do some tests with -fexceptions and see how much effect this actually has...)

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 21:10 UTC (Fri) by daney (subscriber, #24551) [Link]

The test was not with GCC 4.8, so it cannot be used to make assertions about Abstraction Penalties imposed by the GCC 4.8 code base.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 23, 2013 8:57 UTC (Sat) by nix (subscriber, #2304) [Link]

Wasn't it?

... oh hell, it says as much right in the excerpt, and I misread it at least three times. Sigh. Brain failure. You're right, of course.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 18:45 UTC (Fri) by heijo (guest, #88363) [Link]

Amazing news, who would have thought?

An incredible insight: this will result in a sweeping paradigm shift.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 20:27 UTC (Fri) by Yorick (subscriber, #19241) [Link]

I wouldn't call the experiment meaningless. If anything, we need more of these — simple but properly conducted measurements of things of some practical relevance.

Even if nobody who knows how a compiler for C and C++ works is the slightest bit surprised of the results, it's still reassuring, and hard data that can be referred to and used to convince those in the doubt.

Messen ist Wissen!

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 21:31 UTC (Fri) by HelloWorld (guest, #56129) [Link]

> Messen ist Wissen!
Wer misst, misst meistens Mist.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 20:46 UTC (Fri) by Tobu (subscriber, #24111) [Link]

The author does point out GCC's "C as C++" was larger and slower 20 years ago.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 18:48 UTC (Fri) by Tara_Li (subscriber, #26706) [Link]

I would expect that optimally, the exact same machine code should result from the exact same C source file. If not, one or the other has something to learn from its opposite.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 22, 2013 23:07 UTC (Fri) by bmenrigh (subscriber, #63018) [Link]

Surely gcc and g++ don't emit the exact same machine code for the same C source. It's probably *very* close but still slightly different.

I'd love to see somebody compare this though.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 23, 2013 1:30 UTC (Sat) by mveinot (guest, #89989) [Link]

A trivial example I know, but in an uncomprehensive test just now I used both GCC and G++ to generate assembly output (-S param) for a simple "Hello World" C program. The assembly generated for both was identical.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 23, 2013 2:06 UTC (Sat) by Kit (guest, #55925) [Link]

This is in line with what the article author wrote in a comment on his blog:
> Yep… sizes are exactly the same, objdump -d shows only difference is different
> junk in the .notes section.

There might be some corner cases where they produce different code, but if none were exposed by GCC being compiled as C++, then they'll definitely be the exception rather than the rule.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 23, 2013 9:13 UTC (Sat) by khim (subscriber, #9252) [Link]

There are small, subtle, differences. Perhaps the most well-known is the fact that “sizeof 'a'” has size 4 in C, but 1 in C++. This is not big deal since automatic type conversion gives you the same result in the end, but it can easily affect heuristics which will mean that code produced will be equivalent yet different.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 24, 2013 9:29 UTC (Sun) by aleXXX (subscriber, #2742) [Link]

What does --enable-build-with-cxx actually do ?

I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.
Or does that forcce cxx as a language on all C files ?

Alex

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 24, 2013 12:46 UTC (Sun) by jwakely (subscriber, #60262) [Link]

> What does --enable-build-with-cxx actually do ?

Approximately, it does CC=g++

> I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.

I assume you mean sizeof('a') should be 4 if it was considered a C file, but it isn't, using g++ implies the input file is C++

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 10:48 UTC (Tue) by khim (subscriber, #9252) [Link]

I mean, even if I do "g++ main.c", main.c is still considered a C file and sizeof('a') should still be 1.

Rilly? It's easy to test, you know.

$ cat main.c
#include <stdio.h>

int main() {
  printf("%zd\n", sizeof 'a');
  return 0;
}
$ gcc main.c -o main ; ./main
4
$ g++ main.c -o main ; ./main
1

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 0:49 UTC (Tue) by nix (subscriber, #2304) [Link]

Perhaps the most well-known is the fact that “sizeof 'a'” has size 4 in C, but 1 in C++.
What? sizeof(char) is 1 by definition in both languages.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 26, 2013 1:18 UTC (Tue) by andrel (subscriber, #5166) [Link]

But 'a' is not a char in C.

Russell: GCC and C vs C++ Speed, Measured

Posted Mar 27, 2013 15:32 UTC (Wed) by nix (subscriber, #2304) [Link]

Ooh. You're right, I never noticed, it's an int. Bizarrely counterintuitive, though I can see why they did it, I think.

How is it that even a language as simple as C still has corners to learn after decades using it?

C, simple?

Posted Mar 28, 2013 9:27 UTC (Thu) by ncm (subscriber, #165) [Link]

C isn't really simple at all, it's just uncommonly good at presenting the illusion of simplicity. If it really were simple, you wouldn't like it.

C, simple?

Posted Mar 28, 2013 10:01 UTC (Thu) by dlang (✭ supporter ✭, #313) [Link]

As languages go, C actually is pretty simple.

you can do very complex things with very simple building blocks, remember that even the most complex CPU is a combination of AND, OR, NOT gates combined with memory. These are very simple components, but the results that are built with these simple components are very complex.

C, simple?

Posted Mar 28, 2013 17:54 UTC (Thu) by apoelstra (subscriber, #75205) [Link]

> C isn't really simple at all, it's just uncommonly good at presenting the illusion of simplicity. If it really were simple, you wouldn't like it.

As dlang said, the machine model is very simple compared to that of other imperative languages. Lisp and Haskell are probably simpler, but Java or Python are much more complex (and PHP is so wild that I would argue it -has- no machine model!).

By "if it really were simple, you wouldn't like it", ITYM "if it really were simple, it couldn't be used to program computers", because C's complexity seems to be an unavoidable consequence of physical machines (e.g. having finitely sized types).

C, simple?

Posted Mar 28, 2013 19:50 UTC (Thu) by dlang (✭ supporter ✭, #313) [Link]

assembly is simpler, and if you only ever had to program one machine it may even be reasonable, but since assembly is different for every system, and it doesn't have any standard way to define higher level structures, C was born

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds