C++ is faster if you chuck the “safety” features of programming
languages and avoid things like STL, and Boost. In raw bytes to bytes
C++ is faster, but then again so is C.
The moment you add the baggage of STL, and Boost you are slower than
well written C# code. The advantage that the C# JIT and Java jit have
is that those safety features are well optimized. C++ safety features
rely on the optimization of the compiler.
Thus if you are not careful with your STL, and Boost code you will
have a lame duck of an application.
boost和C标准库用于生产极快的生产实现.当然,可以在特定情况下改进这些实现 – 这与编写自己的分配器类似,当您知道您的执行与通用分配器的不同之处,以及如何优化该用法时.所以当然可以分析问题并产生比通用实现(或升级)更快的优化实现.
感谢Justin让我分享他关于封闭问题的答案： – Seth
Quite the opposite.
Boost is not about safety belts.
Boost is about high level software components, with a high level of abstraction This avoids the usual ‘library lock-in’ seen with other frameworks/libraries1.
For example, your Boost Graph library does not require you to switch data structures at all: you can use/adapt any data structure that performs well for your application. In the worst case, you might have to write a traits class to help Boost with the interpretation; It is precisely this trait (common to modern template libraries) that makes Boost perform like nothing else in practice: there won’t be as much library impedance mismatch. This is directly in line with C++11’s new concepts around threading and move semantics: preventing even the most elementary cases of data copying.
Also, all these libraries honour your own allocator implementations, allowing for unsurpassable memory management performance. You can aligned 128bit int vectors in C# – but you’ll have to jump through many many hoops2 and, there is no way you can ever make it work with the framework API’s.
In C++ you pay only for what you use, and Boost is entirely in that spirit.
Mmmm I think I haven’t quite stressed the point enough yet, but I’m done for now.
Let me close by looking at it from the other side: in C# it is much harder to write performant code, because it is much harder to see what’s going on behind the scenes.
Once you drop behind the scenes (unsafe mode, IL code) you’ll arguably be less safe than in C++, because in C++ there is a transparent policy of what happens where and how. In C# you can’t even trust an
int*that you got one line ago (because the Garbage Collector might have moved your cheese); There is no telling what the compiler
and or the JIT engine will make of your nice generic code3.
In short: you can write bad code just anywhere, but Boost cannot be blamed. STL can only be blamed for insane raw performance4.
1PoCo, Qt, MFC, WTL, whatnot….
3Including numerous areas that are close to pathetic and violate the ‘Principle Of Least Surprise’ in a big way (07001)
std::copywill be statically translated in the best implementation based on SSE4, MOVSW or just plain memcpy that money can buy, and yet you don’t have to even write a single letter different from copying a
set, so to speak.