
GCC and C vs C++ Speed, Measured - zorlem
http://rusty.ozlabs.org/?p=330
======
alanctgardner2
Just to be clear, because this confused me at first:

\- GCC recently switched from building their own code as C, to building as
C++. At the moment they only use the subset of C++ which is C, but this
affects compiler behaviour.

\- People complained that C++ programs couldn't be compiled as efficiently as
C programs, and this would make things slower.

\- This post shows that compiling GCC as C, versus C++, does not result in an
appreciable performance change. He benchmarks this by building the kernel
using both versions of GCC.

~~~
popee
Most of C++ devs think that C is part of C++. Even in this document it is
stated something like: C subset of C++ vs C. Why? Because _current_ C is not
subset of C++, C++ is compatible with C89 or C90.

This can look like nitpicking, but it's really annoying to again and again
hear such things from mouth of C++ gurus.

Now give it to me ;-)

~~~
userulluipeste
<https://github.com/libav/c99-to-c89/>

~~~
popee
And this is relevant here because of?

~~~
userulluipeste
...you're twisting my arm now, you know? Why a plain C99-to-C98 convertor it's
relevant? The author explain it better in one of his blog posts:
[https://blogs.gnome.org/rbultje/2012/09/27/microsoft-
visual-...](https://blogs.gnome.org/rbultje/2012/09/27/microsoft-visual-
studio-support-in-ffmpeg-and-libav/)

Quote:

"Reality is, many of these C language features are cosmetic extensions
introduced in C99 that are trivially emulated using classic C89 syntax."

~~~
popee
You just demonstrated another difference in C vs C++ mindset.

C doesn't need so many changes like C++, that is the fact and _advantage_. C++
introduces huge bunch of new stuff, you know add, add, add, then fix, fix,
fix. At the end you get overengineered buerocratic 'give me my 5%' language.

So, in C99 some stuff are very useful like variable length arrays, restrict
keywords, variadic macros... these are not cosmetic. And btw, _even_ _for_
that you don't need C99 to c89 converter, if you want c89 compatibility use
macros. But, hey, macros are ugly, right? They are ugly but they don't make my
eyes bleed as some stuff in C++.

So yes, i'm twisting your arm, sorry about that, but it needs to be twisted
;-)

P.S. Your compiler is your choice, just don't force others to use it.

------
baq
> Using stats –trim-outliers, which throws away best and worse

people make this mistake over and over again. there's no point in taking
anything but the best time when talking about performance in a preemptive
multiprocessing environment. the best time isn't an outlier, every time other
than it is!

~~~
PommeDeTerre
The best time is irrelevant in practice if it only happens very infrequently,
under a very specific set of circumstances.

Benchmarks are only useful if they're indicative of what the likely outcome in
a real-world scenario will be.

What you propose is much akin to those completely unrealistic "The Computer
Language Benchmarks Game"-style benchmarks that show JavaScript (or Java, or
PHP, or whichever slower language you prefer to use instead) as being as fast
as C or C++. Perhaps that's true, but only when you discard reality almost
completely. JavaScript only ends up performing decently because the benchmark
is trivial, and the implementation has been highly tuned, often into a form
that we'd never actually see in any real-world product. And predictably,
JavaScript's performance ends up being much worse than C's or C++'s when it
comes to real software, contrary to what the flawed benchmark suggests.

~~~
cube13
> The best time is irrelevant in practice if it only happens very
> infrequently, under a very specific set of circumstances.

This isn't a practical benchmark, though. This is an experimental performance
comparison. So to ensure that your results aren't influenced by other
variables, it's a good idea to attempt to produce the ideal environment for
best performance across both tests. So binding cores(to eliminate as much
variance from Linux's thread scheduler) and running nothing else on the
machine are actually good decisions to make here.

Comparisons to the Computer Language Benchmarks Game is somewhat irrelevant
here, because the comparison being run is actually extremely simple: Using a
GCC executable compiled as C versus a GCC executable compiled as C++. The
traditional failings of the game aren't here(implementation issues, language-
specific tuning issues, etc.) because the code that GCC is compiled from is
identical, the compiler options should be identical(except for C vs C++
compile setting), and the test setup should be run on the same machine, with
the same compiler options and tuning choices.

------
davidw
FWIW, RustyRussell is this Rusty: <http://en.wikipedia.org/wiki/Rusty_Russell>

Nice to see him here - "ciao" from Italy:-)

~~~
16s
Yes. We can blame the ipchains/iptables syntax on him ;)

Also, he used to poke fun at C++ (many older C devs did that) but more and
more of them are coming around to C++. The last automobile systems programmers
I worked with were switching to C++ as well. In ten years, C++ will be the new
C.

~~~
RustyRussell
I'm not coming around to C++, I assure you.

But I like measurements, even if they don't confirm my baises.

------
alexk7
The results are what I expected and not that interesting. Compiling C as C++
usually yields the exact same native code as compiling it as C, except for
some very minor differences.

What would be interesting is how long it now takes to compile GCC itself. For
code that does not use C++ features, I also expect a very minor difference.

~~~
jedbrown
Compilation of PETSc, which is written in the intersection of C and C++, takes
twice as long using a C++ compiler as using a C compiler. I'd be interested in
the time difference for compiling GCC.

------
willvarfar
Interesting aside: There are some things that are faster in C++ than C, for
example the standard library sort functions are massively faster because the
compiler can in-line the comparison.

~~~
jedbrown
The performance is the same if you put the qsort implementation in a header.

<http://www.agottem.com/blog/inlining_qsort_sort>

~~~
malkia
or use link-time compile generation (slow, and it might not work for huge
projects like gcc/webkit, but if split into shared modules it can)

------
adamnemecek
That is one hell of a misnamed article.

~~~
dbaupp
Why? It is measuring the difference between a compiled-as-C GCC and a
compiled-as-C++ GCC.

~~~
adamnemecek
Difference in compilation times.

~~~
RustyRussell
Yeah, people seem confused by this. You can build the last release of gcc
either with a C++ compiler or with a C compiler. I did both, and benchmarked
the results.

Hope that helps! Rusty.

~~~
adamnemecek
Sorry, I missed the sentence: "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."

------
lysium
I don't think having a single statistics point is enough to make a judgement
here. There are many different programs of which compilers are a very small
and relatively rarely used subset.

------
eliben
So 0.3% is a measurable difference and 0.1% is noise. That doesn't sound
overly pragmatic to me. Benchmarking is an art, and from his setup 0.3% seems
like noise as well.

------
tosseraccount
Aren't the files already cached in memory by the time the C++ compilation
happens? Wouldn't flushing memory provide a better measure?

------
k__
I always ask myself, why are there people who put statistics without charts on
the internet.

~~~
RustyRussell
Because there are only two stats of note in the post, and they are 0.3% and
0.1%. That kind of difference doesn't make for useful graphs, does it?

~~~
misnome
They did 40 runs of compilations (10 each, twice). At the very least, a
scatter or bar graph would be helpful, as it would let you see the data
distribution (those numbers are good as uninterpretable on their own).

Saying that, 40 isn't very many times to draw statistical conclusions so you'd
probably need more data.

~~~
aaronharnly
I think a histogram or boxplot might be more appropriate to show the
distribution. A scatter plot isn't appropriate for data whose independent
variable is categorical (unless you jitter), and a bar plot (unless a
histogram) would just show the means, wouldn't it?

------
tlarkworthy
So python is the fastest?

~~~
caf
We don't know, because GCC does not execute as a Python program.

This _wasn't_ testing the speed of compiling a program as C++ versus compiling
it as C; it was testing the speed of compiling the Linux kernel using GCC-
compiled-as-C++ as the compiler versus using GCC-compiled-as-C as the
compiler.

(And it concluded that the difference was insignificant anyway!)

~~~
tlarkworthy
Yeah my bad I thought we were measuring the speed of compilation in C Vs. C++.
(looks like it wasn't just me)

------
jedbrown
What is the time difference to compile GCC itself?

------
z3phyr
Linus will now use LCC

------
papsosouid
People were complaining that the binaries would be slower? All the complaints
I heard were "now it will take 10 times longer to compile gcc", not "now gcc
will run slower".

