News for hackers. Stuff that matters.
Re:Is C++ fast? by throwawaymath on Friday January 18, 2019 @09:08AM (#18939362)
I interpret the title to mean, "Is standard usage of C++ language features and the STL competitive with C?". The author is basically showing how you improve performance by selectively tearing out more and more features C++ hands you out of the box. This is characteristic of writing C++ in a more C-like style. In that sense I think the author is correct.

However I'd also say this is something already well known by most C and, particularly, C++ developers. Yes, you can get better performance by reimplementing much of what C++ gives you, and that is typical of writing C code instead of C++. But there's a fair bit of productivity trade off in doing so.

Re:Is C++ fast? by vidarh on Friday January 18, 2019 @09:41AM (#18939571)
This is one of the defining principles of C++, after all: You pay only for what you use. Conversely, if you do opt-in to using more advanced features, it may come at a cost, but it's all optional.

It's been one of the things that have most strongly shaped the evolution of the language and the library, for both good and bad, so absolutely, it is very much well known.

Re:Is C++ fast? by jura_z on Friday January 18, 2019 @03:43PM (#18942876)
'You pay only for what you use' Sorry, have you read the article? He is not using any C++20, C++17, C++14... features, just adding a compiler key. And he's paying with compilation time. https://twitter.com/zeuxcg/status/1085781851568914432

You might say that compilation time is nothing.. But it is C++, compilation time has a huge impact on big codebases.

Re:Is C++ fast? by vidarh on Friday January 18, 2019 @05:24PM (#18943674)
He started out with among others, std::vector. That has tradeoffs, in that the standard specifies requirements that he may or may not need.

My point stands. The performance trade-offs you're paying for with various C++ features are extremely well understood, and better documented for C++ than most languages.

That doesn't mean there aren't surprises here and there, and obviously there are compiler difference, but the point was that the language is designed so that these tradeoffs are known and documented and for the most part comes as a surprise to people mostly when they don't know the language very well.

It is not a surprise that you can improve on things like std::vector by rolling your own that does exactly what you want; for starters if you know your requirements chances are you can speed up initialization, because you don't need to abide by standard semantics. For starters, if you know you're going to assign a fixed number of elements, you don't need to track size and capacity, and you can often avoid the need for bounds checking.

As for compilation time, that too is very much a matter of paying for what you're using. You can choose to disable debug info, you can choose to not include headers that are bound to result in lots of time spent on parsing templates.

That he's not using many advanced features is irrelevant; his starting point very much had made trade-offs in using functionality that comes with known costs.

Re:Is C++ fast? by dfox on Friday January 18, 2019 @12:20PM (#18940859)
For me this principle is also what often causes performance issues, because for many advanced features this leads to convoluted language semantics and sub-optimal implementations.
Re:Is C++ fast? by deng on Friday January 18, 2019 @09:15AM (#18939401)
I'm pretty sure I could make a faster 'printf' but which only supports '%d'. Does that mean 'C is slow'?
Re:Is C++ fast? by throwawaymath on Friday January 18, 2019 @09:42AM (#18939572)
No, not at all! At least not in an absolute sense. But what I mean is that writing in C naturally tends towards writing more special-purpose, bespoke functions because you can't lean on the existing, general-purpose functions of C++ (nor language features).

I agree with you about the title, but I also think that if you fix the title the author is making a good point. It's not groundbreaking, but the move "down" from C++ to C does run parallel with the move from more general purpose to more special purpose, in a lot of ways. C dominates embedded after all.

Re:Is C++ fast? by simias on Friday January 18, 2019 @09:36AM (#18939535)
I'd say that printf, with its own DSL, is actually pretty slow. It's meant for debugging or "slow" I/O, if you need to write a lot of data it should be avoided.

A few years ago for debugging purposes I wanted to hexdump a large amount of data. Turns out that the standard hexdump tool was pretty slow at it, writing a simple C utility that would format the dump "by hand" instead of using printf was about an order of magnitude faster. You have fully static code instead of the interpreted format string, you can unroll all the formatting, you don't have to rewrite the characters that don't change in every line (spaces, delimiters etc...). It's lightning fast but of course not very flexible.

Re:Is C++ fast? by deng on Friday January 18, 2019 @09:41AM (#18939567)
> I'd say that printf, with its own DSL, is actually pretty slow.

Yes, of course it is. That's why I took it as an example. Since I work on RealTime systems I usually have to use plain 'write' instead. But that is because 'printf' is an incredibly complex function, not because 'C is slow'.

Re:Is C++ fast? by simias on Friday January 18, 2019 @09:54AM (#18939655)
At this point I feel like it's almost a philosophical argument but:

- printf is a standard function in C's stdlib.

- Because of the nature of the function (using a special purpose format strings and varargs instead of some kind of generic system) the function can't really be aggressively optimized and inlined without compiler magic. This limitation is caused by C's own limitations when it comes to generic programming and type introspection, printf needs to be told explicitly through a "side-channel" (the format string) what types to expect because C lacks the infrastructure to do that by itself. It also means that you can't extend the function for custom types and that you can't type-check the parameters without compiler magic.

- Even though some compilers do implement special magic around printf to perform more aggressive optimizations (like replacing it with "puts" if there's no formatting going on for instance) it's still generally trivial to beat its performance in non-trivial case with handwritten code.

Given all of the above I would say that makes this particular corner of C rather slow indeed. Or at least slower than it might be compared to an hypothetical language that made it easier to optimize string formatting.

Re:Is C++ fast? by skrebbel on Friday January 18, 2019 @12:44PM (#18941102)
You're entirely right, but I feel like you're trying really hard to misunderstand deng's original comment. It's an apt analogy, partly because of what you wrote. This article's title is as ridiculous as an article titled "Is C fast?" with a story about a faster_printf that only supports %d.
Re:Is C++ fast? by ghettoimp on Friday January 18, 2019 @11:19AM (#18940321)
It's been a long time since I've done C++. I wonder if

   int x;
   cout << x;
ought to be faster than printf("%d", x), by virtue of knowing the types ahead of time?
Re:Is C++ fast? by Someone on Friday January 18, 2019 @03:24PM (#18942699)
Pedantic: No. If you write

  #include “studio.h”
  int x;
  printf(“%d”, x);
the compiler knows the types ahead of time, too, so that cannot explain any speed difference (and yes, C compilers do try to avoid the generic printf in the library. See http://www.ciselant.de/projects/gcc_printf/gcc_printf.html for examples)

(The #include is essential. With it, the compiler can know what printf does, and, in theory, optimize it to a int-to-string conversion and a puts system call. Without it, it cannot, because it doesn’t know what the printf function does (in this case, it is easier to generate fast code for the compiler if it doesn’t have the source available than when it would see source code for a function called printf)

Utterly pedantic: no. As given, both are undefined behavior, and it is trivial for a compiler to discover that.

Re:Is C++ fast? by tom_mellior on Saturday January 19, 2019 @10:08AM (#18947386)
> and yes, C compilers do try to avoid the generic printf in the library

That is true for certain format strings (containing only %s or %c conversions), but for %d GCC and Clang don't seem to want to call a print_int function, possibly because there is none: https://gcc.godbolt.org/z/rltFse

Re:Is C++ fast? by smitherfield on Friday January 18, 2019 @02:34PM (#18942201)
Yes, but only if you first call

  ios::sync_with_stdio(false);
https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdi...
Re:Is C++ fast? by octo_t on Friday January 18, 2019 @12:44PM (#18941095)
If it weren't for locales, probably yes ;P
Re:Is C++ fast? by chriswitts on Friday January 18, 2019 @11:16AM (#18940297)
You could check out Nanolog [1] for inspiration.

[1] https://github.com/PlatformLab/NanoLog

Re:Is C++ fast? by carlmr on Friday January 18, 2019 @09:56AM (#18939676)
That's why you measure first, and only if necessary go to these extreme measures if you find that this is the place that really costs you performance.

Or if performance is basically your only goal (HFT) then you just need to spend this time on a lot of parts of your code.

But then in 99% of code it's a waste of time.

Re:Is C++ fast? by Sharlin on Friday January 18, 2019 @01:57PM (#18941826)
The C++ standard library is pretty competitive with C given that C has no standard data structure and algorithms library! It's not even an apples and oranges comparison.
Re:Is C++ fast? by proginthebox on Friday January 18, 2019 @10:21AM (#18939862)
With proper use of constexpr, templates and types, you can make C++ at least as fast as C code without giving up on readability.
Re:Is C++ fast? by stochastic_monk on Friday January 18, 2019 @03:41PM (#18942851)
At least as. Often faster because of easier inlining.
Re:Is C++ fast? by geezerjay on Friday January 18, 2019 @01:18PM (#18941404)
> The author is basically showing how you improve performance by selectively tearing out more and more features C++ hands you out of the box.

In that case the author is showing what has been widely known by C++ developers for decades. In fact, the "don't use the STL because it's slow" is a mantra which, along with the old "replace STL's default allocators with your customized ones", is repeated ad nauseum in gamedev circles.

Re:Is C++ fast? by vectorEQ on Friday January 18, 2019 @09:11AM (#18939380)
if you don't use any of the slow language features it's fast
Re:Is C++ fast? by Retra on Friday January 18, 2019 @09:21AM (#18939440)
If you use special-purpose tools rather than robust ones, you can get better performance. But nobody wants to ship a language without robust general-purpose tools.
Re:Is C++ fast? by krapp on Friday January 18, 2019 @12:22PM (#18940876)
Even the slow language features are fast enough for most use cases, though. I think the actual comparison here isn't between 'slow' and 'fast' but 'probably fast enough' and 'as fast as possible.'