News for hackers. Stuff that matters.
Re:Is C++ fast? by lultimouomo on Friday January 18, 2019 @08:53AM (#18939264)
So if you get rid of bounds check, reimplement your own feature-limited vector and hash table and change the algorithm moving from a general purpose exact sort to an inexact, domain-specific one, you can improve your run time from 572ms to 320ms.

What does this have to do with C++?

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.'
Re:Is C++ fast? by lucozade on Friday January 18, 2019 @09:26AM (#18939464)
In fairness to the article, it is putting some flesh on the bones of an area that I have seen cause surprise.

It's a variant of the observation that, although in C++ you aren't paying a direct abstraction cost, you are often paying a generalisation cost. This cost is obscured by the abstraction and may not be warranted in your application. In other words, an indirect abstraction cost.

Re:Is C++ fast? by CoolGuySteve on Friday January 18, 2019 @08:57AM (#18939287)
Ya after changing the hash table and sort algorithms, the rest of the improvements are fairly minor (other than MSVC’s debug performance).

This is more like “is C++’s STL fast for me”.

Re:Is C++ fast? by lultimouomo on Friday January 18, 2019 @09:00AM (#18939304)
For me the proper title of the article would be "std::unordered_set is slow and MSVC debug runtime is even slower", but I guess that wouldn't generate as many views as pretty much everyone knows it.
Re:Is C++ fast? by mynegation on Friday January 18, 2019 @09:53AM (#18939654)
Came here to say almost exactly this. C++ is fast. All the things mentioned in the article are very specific replacements of C++ standard library implementations. C++ is a zero cost abstraction language, all these things could have been replaced with idiomatic C++. The fact that they were implemented in C is more of an authors language preference. Reimplementing some things might be easier in C because C is a simpler (and often more familiar) language. Idiomatic C++ may require knowledge of pretty arcane feature of already complicated language, like placement new and judicious use of references and move operators.
Re:Is C++ fast? by FreezerburnV on Friday January 18, 2019 @10:18AM (#18939838)
I said this elsewhere, but I would legitimately like to see a program like what the author wrote, written in idiomatic C++ that performs as well as what the author got to. Maybe in a comparison style like the author to show the slower C++, the faster "C with classes", and the comparable speed "idiomatic C++" or something. I feel like I see people saying that idiomatic C++ is just as fast as something, while other people say that idiomatic C++ is slower and should be replaced. I'm going to assume you can for sure write C++ using the STL to be just as fast, and would like to learn what needs to be done to get to that point so I can incorporate those techniques into my own C++ code where I might need that extra bit of performance. (I'm playing around with writing games in C++, so performance is more of a concern than an IO-heavy web server. Or I'm considering learning QT for making apps, etc.)
Re:Is C++ fast? by mynegation on Friday January 18, 2019 @12:50PM (#18941155)
Author posted C version of their code[1]. As a couple of examples of how you can go back to idiomatic C++ starting from here: 1. replace all pointers by passing references to templatized vectors. On a high level of optimization they will be compiled to the same pointer arithmetic as C, but will give you type-safety on the source level. 2. Replace structs with clasess with short inlined accessors. On a high level of optimization they will be compiled to the same memory offset access 3. _If_ there is any value in making domain-specific sort generic - make it such

[1] https://gist.github.com/zeux/bf847986e0474cf48f61bb5749da38e...

Re:Is C++ fast? by stephencanon on Friday January 18, 2019 @10:08AM (#18939760)
All the things that were replaced were abstractions, which had costs. I think that there's a valid point that a lot of C++ programmers use these abstractions liberally, while sweeping the very real costs under the rug. There are also benefits to using them, of course.
Re:Is C++ fast? by lasagnaphil on Friday January 18, 2019 @10:13AM (#18939809)
I would say C++ is more like a zero runtime-cost abstraction language (in release builds).

You could achieve good release build runtime-speeds in modern idiomatic C++, but you have to trade off your compile times and Debug-build runtime-speeds for that.

Re:Is C++ fast? by vardump on Friday January 18, 2019 @11:08AM (#18940236)
> I would say C++ is more like a zero runtime-cost abstraction language (in release builds).

Well, other than the "zero-cost" features that increase code size. For example careless use of heavy template classes can generate a lot of code.

In other words, in C++ it's very easy to generate a lot of code from seemingly innocent constructs.

Instruction cache misses, page faults (and to lesser amount TLB misses) are not zero cost.

Re:Is C++ fast? by ben509 on Friday January 18, 2019 @12:31PM (#18940980)
To your point, cost (or strength) should always consider size as well as time.

It would be nice to have a tool that could compute the size impact of a given line.

Re:Is C++ fast? by jblow on Friday January 18, 2019 @12:17PM (#18940827)
> What does this have to do with C++?

Given that most new C++ features in the "modern day" are implemented as std::whatever, in the "everything is a library" way, it's extremely relevant.

Re:Is C++ fast? by Ace17 on Friday January 18, 2019 @02:15PM (#18942014)
Just because I'm using a C++17 compiler doesn't mean I'm happy with the standard library.

I still have to google every time I need to sleep the current thread ; and don't get me started on iostream!

However, I'm happy to be able to return a buffer without copying it (move semantics), to precompute frequency tables at compile time (constexpr), to be able to pass around functions without having to create a dedicated class (lambdas), not to have to worry about having an extra conversion (auto) ... And soon, I will be happy to write serialization code without relying on dirty tricks (introspection).

Re:Is C++ fast? by jcranmer on Friday January 18, 2019 @09:14AM (#18939395)
Well, I can imagine there are some languages where the interpreted nature of the language keeps you from being able to build higher-performance highly-specialized data structures and algorithms that outperform the native implementations (say, JS). But even then, it still doesn't justify the title.
Re:Is C++ fast? by brohee on Friday January 18, 2019 @09:24AM (#18939455)
It sure shows that the sentence "you only pay for the features you use" is wrong at least.
Re:Is C++ fast? by SeanBoocock on Friday January 18, 2019 @09:38AM (#18939552)
What? It demonstrates the opposite. He stopped using general purpose containers and algorithms, and replaced them with implementations more tailored to his use case. He didn't keep paying for std::sort or std::unordered_set once he stopped using them. By contrast in most managed languages with a GC, I can't opt out of the GC even if I want to take responsibility for memory management and object lifetime.
Re:Is C++ fast? by brohee on Friday January 18, 2019 @10:38AM (#18939980)
> The issue is that math.h includes cmath in gcc/clang which pulls in a lot of C++ machinery, and in C++17 in libstdc++ adds a slew of new special functions, that are rarely useful but will make compilation slower anyway.

> This is definitely an area where libstdc++ and libc++ could improve in the future - I don’t think it’s reasonable to force users of C headers to pay for the C++ baggage.

Re:Is C++ fast? by eMSF on Friday January 18, 2019 @11:04AM (#18940189)
You're being obtuse on purpose. The sentence you claim is wrong has only ever been used to describe performance at run time.
Re:Is C++ fast? by rurban on Friday January 18, 2019 @11:27AM (#18940399)
Most managed language runtimes I know allow you to opt out of a GC, or to tune it to your workload.
Re:Is C++ fast? by jungler on Friday January 18, 2019 @01:15PM (#18941368)
That's different from "I do not want a disabled GC baked into my program." It is relevant, especially to embedded coding with its ornery size restrictions.
Re:Is C++ fast? by rurban on Saturday January 19, 2019 @04:16AM (#18946128)
In embedded environments the GC is fundamentally different to a large RAM machine. There a simple boehm-gc is enough and still faster, safer and smaller than refcounting. It can be made incremental.

On big machines a copying collector with a nursery is much faster and safer if you can deal with the pauses.

Memory safety is still an issue I heard.

Re:Is C++ fast? by kiriakasis on Friday January 18, 2019 @09:27AM (#18939471)
when he stopped using them he also stopped paying for them
Re:Is C++ fast? by MichaelMoser123 on Friday January 18, 2019 @09:30AM (#18939498)
C++ comes with the OOP mindeset: OOP is about polymorphism, inheritance and encapsulation. Now what if you are abstracting over the wrong library for a given task? Will you be able to question your basic assumptions, or is that an assumption that is not allowed to be questioned? (due to the encapsulation principle)
Re:Is C++ fast? by thestoicattack on Friday January 18, 2019 @09:39AM (#18939558)
I don't think C++ comes with the OOP mindset. At least, not recently. Most people will tell you it's "multi-paradigm." And I would say C++ certainly gives you enough flexibility to dig underneath the abstractions, as TFA shows.
Re:Is C++ fast? by MichaelMoser123 on Friday January 18, 2019 @09:44AM (#18939587)
I am not so sure about the multi-paradigm thing: In c++ everything is a class. Also of you try hard enough then you may implement any paradigm in any language.
Re:Is C++ fast? by lultimouomo on Friday January 18, 2019 @09:47AM (#18939615)
You mean except booleans, integers, floats, pointers, free functions, namespaces, templates and lambdas?
Re:Is C++ fast? by gpderetta on Friday January 18, 2019 @10:57AM (#18940134)
Also a class in C++ is by default exactly the same as a struct (except for default access control which is a minor thing). An OOP style class would be any class type (including struct) with virtual functions which you would have to opt in.
Re:Is C++ fast? by MichaelMoser123 on Monday January 21, 2019 @06:01AM (#18959474)
yes, you can do a C++ program with functions only + integers and array of integers. technically that's C++. Very apt observation! I still guess that the main entities in your c++ program will be classes (not POD)

And I still maintain that most people treat class libraries (such as STL) as given and do not inspect how they are implemented and do not consider what constraints / alternatives do exist. One example: the std::dequeue is a dynamic array, and you can't quite tune the size of the inner array easily by means of template or instance parameters - no one cares. Also many people happily use the shared_ptr class and don't even know that it takes very expensive atomic operations to do the reference counting; I would have expected a template parameter that could determine if shared_ptr is meant for multithreaded use or not. Most people don't care and happily use the shared_ptr. Also there are no intrusive linked lists in STL (well, there are in boost)

I can go on and on about the STL (and other widely used libraries if you care) and I still suspect that they get away with these "issues" in a widely used library because people are used not to question library implementations and treat them as frigging black boxes.

Re:Is C++ fast? by asveikau on Friday January 18, 2019 @03:43PM (#18942873)
I agree with your sentiment but the lambdas do get treated as anonymous classes overloading operator ().
Re:Is C++ fast? by kllrnohj on Friday January 18, 2019 @05:32PM (#18943738)
Not really true. Lambdas can be converted to a class overloading operator() (such as std::function), but they are not inherently that. You can use them as just function pointers, too. Example:

    #include <cstdio>

    void call_func(void(*func)()) {
        func();
    }

    int main(int argc, char* argv[]) {
        // Lambda can be converted to a function pointer
        call_func([]() {
            printf("hello as a function pointer\n");
        });
    }
Re:Is C++ fast? by asveikau on Friday January 18, 2019 @05:54PM (#18943899)
> but they are not inherently that.

I am fairly certain I have had error messages and disassemblies that suggest otherwise. This may however be an implementation detail.

> You can use them as just function pointers, too.

Yes, I know this. It only works when there are no captures. And AFAIK once there are captures you start getting object code that looks a lot like you put those captures into an anonymous class. (When not inlined.)

Re:Is C++ fast? by slavik81 on Friday January 18, 2019 @11:24PM (#18945319)
You can get that sort of behaviour from any class by implementing the conversion operator. I assume that lambda implements `operator(void(*)())` so it is implicitly convertible to a function pointer.
Re:Is C++ fast? by thestoicattack on Friday January 18, 2019 @09:46AM (#18939600)
That's Java, isn't it? I write lots of C++ without classes.
Re:Is C++ fast? by mindcrime on Friday January 18, 2019 @10:34AM (#18939951)
Even though Java is still very "class-centric", in the strictest sense Java doesn't make "everything a class". There have always been primitives for things like int, long, boolean, float, double, etc. And now that we have MethodHandles and what-not, you can (sort of) start to think of functions as first class entities in Java. And there are Lambdas as well which effectively let you build a function without it needing a class to contain it.
Re:Is C++ fast? by thestoicattack on Friday January 18, 2019 @10:49AM (#18940067)
I was being a little flippant and unfair to Java because I was trying to find where the claim "everything is a class" had appeared from.
Re:Is C++ fast? by Sharlin on Friday January 18, 2019 @02:03PM (#18941895)
The "STL" part (data structures and algorithms) part of the C++ standard library uses no OO design at all. The iostreams part does, but it's hardly visible to the user. (No, simply having syntax that allows you to write `data.function()` instead of `function(data)` doesn't make anything object-oriented.)
Re:Is C++ fast? by lultimouomo on Friday January 18, 2019 @09:45AM (#18939596)
There is exactly 0 polymorphism and very limited inheritance in the code referenced by the article.

And STL really isn't based on an OOP mindeset.

Re:Is C++ fast? by pdpi on Friday January 18, 2019 @10:07AM (#18939751)
There is exactly zero _subtype_ polymorphism. The STL uses templates as a means to achieve parametric polymorphism (i.e, generics).
Re:Is C++ fast? by MichaelMoser123 on Friday January 18, 2019 @12:57PM (#18941214)
Yeah, and zero encapsulation also. Keep up the good work!