14 ms·
GCC and C vs C++ Speed, Measured
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- adamnemecek 14y agoThat is one hell of a misnamed article.
- dbaupp 14y agoWhy? It is measuring the difference between a compiled-as-C GCC and a compiled-as-C++ GCC.
- adamnemecek 14y agoDifference in compilation times.
- capisce 14y agoWhich is indicative of performance.
- adamnemecek 14y agoBut isn't GCC still written mostly in C? So would not both gcc versions be expected to be very similar on instruction level?
- masklinn 14y ago> So would not both gcc versions be expected to be very similar on instruction level? It's going to go through the g++ frontend, parsing and compiling it using C++ semantics. There's no guarantee that the C subset of C++ will yield the exact same result as that of an actual C compiler. And a decade or two ago, as TFA notes, it most definitely did not.
- adamnemecek 14y agoI guess I just assumed that considering that both compilers are part of the same compiler collection that their output, while not guaranteed to be the same, can be expected to be somewhat similar. And that the divergences should not have all that much of an impact on execution speed. But I guess I did jump to a conclusion there.
- masklinn 14y agoThe backend should do the same thing, but if the C++ frontend gives complete garbage to the backend, I guess there's only so much the backend can do to fix the mess.
- caf 14y agoIt's measuring the speed of the GCC-compiled-as-C compiler against the GCC-compiled-as-C++ compiler by measuring how long each of those takes to compile the Linux kernel. It's not measuring how long it took to compile GCC itself.
- RustyRussell 14y agoYeah, 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 14y agoSorry, 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."
- masklinn 14y agoTo test a compiler's performances, you use it to compile stuff don't you? That's what TFA does: it builds GCC 4.7 using both a C compiler and a C++ compiler, then looks how the results perform, by compiling stuff with them. TFA did not bench how long the original compilation (whether through C or through C++) took, that would have been pointless and irrelevant.
- easytiger 14y agoi think the point is largely it is irrelevant. people care abut the speed of their process, not the time it took to generate (within reason)
- caf 14y agoIn this case the process is "GCC" and the way to measure its speed to measure how long it takes to compile something else.
- easytiger 14y agoyes but the point was the title is ambiguous because almost all people will care about the speed of the binary it outputs not it itself. Indeed that is something people spend a lot of time on
- k__ 14y agoI always ask myself, why are there people who put statistics without charts on the internet.
- RustyRussell 14y agoBecause 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?
- baq 14y agomost of the text could be replaced by a bar chart.
- misnome 14y agoThey 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 14y agoI 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?
- z3phyr 14y agoLinus will now use LCC
- deleted 14y ago[deleted]
- cperciva 14y agoUnless you're building OpenOffice, everything is cached after the first compile finishes. Compiling hasn't been I/O bound for several years.
- RustyRussell 14y agoMy kernel compiles are definitely not I/O bound :) The CPU sits close to 100% for most of the compile, running cc1 as you'd expect.
- kryptiskt 14y agoIt's certainly not IO-bound. Think about the Linux kernel. It's compiling on the order of 100MB of source code into something of the same order of magnitude of binary objects, and it takes minutes. That is a not close to saturating any kind of disk device. Not to mention that the intermediate files will be cached in RAM by the file cache and can be used without them hitting the disk.
- Tobu 14y agoMy kernel directory is 13GB, most of that is intermediate build products. Linus has written somewhere on how much faster kernel compilation is with an SSD.
- kryptiskt 14y agoMy dual-SSD striped setup with humongous throughput (600MB/s) made a kernel compilation 1.4% faster compared to using a 5400rpm HD. But then I have 16GB RAM, and RAM is a huge bottleneck in its own right, as swapping or pushing out the file cache will kill compilation times.
- Tobu 14y ago> I don't think compilation is CPU-bound - it's probably closer to 50/50 between CPU & I/O, if not outright I/O-bound. So, is compiling the kernel really a good benchmark for what you're trying to measure? > The result is in line with what I'd expect, though :) > [EDIT] Well, it depends: http://stackoverflow.com/questions/15199356/speed-up-compile-time-with-ssd http://stackoverflow.com/questions/15199356/speed-up-compile... On first run without an SSD it certainly would be (linking the modules of a distro kernel, particularly so). The rest depends on rusty's setup, and in 14 minutes, it looks like it was a trimmed down build that could fit in memory.
- tlarkworthy 14y agoSo python is the fastest?
- caf 14y agoWe 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 14y agoYeah my bad I thought we were measuring the speed of compilation in C Vs. C++. (looks like it wasn't just me)
- davidw 14y agoFWIW, RustyRussell is this Rusty: http://en.wikipedia.org/wiki/Rusty_Russell http://en.wikipedia.org/wiki/Rusty_Russell Nice to see him here - "ciao" from Italy:-)
- 16s 14y agoYes. 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.
- malkia 14y agoas long as you keep extern "C" in your api...
- RustyRussell 14y agoI'm not coming around to C++, I assure you. But I like measurements, even if they don't confirm my baises.
- baq 14y ago> 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!
- hartror 14y agoWhat is the reasoning?
- lolcraft 14y agoThe best time measured is the time of execution with least switching to other processes. So, it's actually the closest to the real time.
- fulafel 14y agoNo, there are other things which affect timing besides switching to other processes. For example, virt:physical mapping of the process memory affects cache performance unpredictably.
- fulafel 14y agoNo, there are other things which affect timing besides switching to other processes. For example, virt:physical mapping of the process memory affects cache performance unpredictably, see http://d3s.mff.cuni.cz/publications/download/hocko-sipew10.pdf http://d3s.mff.cuni.cz/publications/download/hocko-sipew10.p...
- PommeDeTerre 14y agoThe 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.
- lysium 14y agoI 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.
- alanctgardner2 14y agoJust 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.
- niggler 14y agoI was under the impression that people were complaining about compilation speed AND the optimizations available to a C++ compiler (even if it was targeting the C subset). I wish the article gave some information about the runtime difference.
- twoodfin 14y agoThe article is about the runtime difference. In this case, the "run" is using gcc's compiled in both languages (via gcc and g++) to build the Linux kernel. Both gcc's show about the same runtime performance when executing this task. I can see how this is a little confusing but it shouldn't be that hard. And yet there have been hours of comments on this story that miss this point.
- niggler 14y ago"AND the optimizations available to a C++ compiler (even if it was targeting the C subset). I wish the article gave some information about the runtime difference." To be absolutely explicit, because I realize my phrase wasn't clear, I was under the impression that C++ optimizations were expensive (say, under -O3 or -Os), so I wish that the same test were run under -O3 or -Os (comparing both the speed of compilation AND the optimizations)
- mpyne 14y ago> I was under the impression that C++ optimizations were expensive (say, under -O3 or -Os), so I wish that the same test were run under -O3 or -Os (comparing both the speed of compilation AND the optimizations) What optimizations are available to the C subset of C++ that are not also available to C? The only thing I can think of offhand which would make compilation of C-with-C++ slower is the difference in symbol naming. From what I understand of GCC, once the compilation has progressed to the optimization stage the code has already been converted to an intermediate language so the source frontend (gcc or g++) should not necessarily make available extra optimization passes. Again, this is speaking strictly of the subset of C which is common to both C and C++.
- jedbrown 14y agoWhat is the time difference to compile GCC itself?
- eliben 14y agoSo 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.
- alexk7 14y agoThe 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 14y agoCompilation 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.
- tosseraccount 14y agoAren't the files already cached in memory by the time the C++ compilation happens? Wouldn't flushing memory provide a better measure?
- papsosouid 14y agoPeople 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".
- willvarfar 14y agoInteresting 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.
- aninteger 14y agoWell, yes you are correct. However if you need the fastest sorting you're probably gonna inline the comparison regardless of language. If qsort is slow due to the function pointer comparison just move it inline manually.
- jedbrown 14y agoThe performance is the same if you put the qsort implementation in a header. http://www.agottem.com/blog/inlining_qsort_sort http://www.agottem.com/blog/inlining_qsort_sort
- malkia 14y agoor 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)