Is C++11 is faster than C++03 in terms of efficiency?

Viewed 2408

I work in trading firm and here latency matters a lot. The project assigns to me is developed using a mix of c and c++98 parts, But I believe we can make same project using C++11 without losing efficiency. As discussed with my seniors, they say you should stick with C and c++03 as they are efficient compare to C++11 at micro level. Can anyone highlight me If I go with C++11, will I get better results?

4 Answers

C++11 is faster, because moving of objects was introduced. Mainly the usage of this feature in the STL speeds up some applications a lot without any code change in user code. Applications can be programmed much more efficient then before. Also constexpr construction can result in much faster application startup, because objects can reside in flash space on small controllers instead copy them into ram. There are a lot more features which help to get the code more efficient. For example emplace_back for conatainers help to generate objects in place instead of creating & copy them.

C++17 introduces guaranteed copy elision, which speeds up also in a lot of use cases.

This is mostly wrong.

Firstly, if you give C++03 compliant source to a compiler like GCC, it is very unlikely that the generated machine code will be any different if you specify --std=c++03 compared to --std=c++11.

Secondly, using features like auto and "range based for" will be neutral for efficiency. (There may be a few cases where Range-based-for will allow the compiler to optimize the evaluation of the termination condition more efficiently than a naive loop, but these will be rare.)

Thirdly, there are some features (like move semantics) which are actively beneficial for efficiency.

Finally, there are a few cases where naively written C++11 will be less efficient than the equivalent C++03 code. For example:

std::vector<std::vector<big_struct>> big_2d_array;
for (auto v : big_2d_array)
    do_stuff(v);

This will copy v and will be expensive. It needs to be:

for (auto &v : big_2d_array)
    do_stuff(v);

Note the reference. (I would also recommend const, but that's a separate issue).

Honestly, this is highly dependent on the specific code snippet. C++11 is only a newer revision of the C++ language standard (ISO/IEC 14882:2011).

A newer revision only changes the grammar of certain expressions and statements, and usage of certain keywords, as well as introducing new (and useful) stuffs, like rvalue reference (T&&), auto type deduction (auto and decltype), variadic template parameters (template <typename... Args>) and so on. In spite of the fact that some introductions may help you write more efficient codes (e.g. move semantics), it essentially does not change the way compilers are required to generate CPU instructions from C++ source codes.

So in micro-instruction level, the compiled instructions from the same source would mostly remain the same under C++98/0x/11, so there should not be any observable difference in performance.

What in fact matters the more is the algorithm you choose and the specific implementation you write, as well as compiler optimization (usually -O# command line argument). With new standards, you are allowed to write faster codes with move semantics, range-based for loop, decltype(auto) (this one is C++14, though)


In fact, this code generates exactly the same assembly code, when language standards is the only different option supplied to the compiler:

#include <iostream>
using std::cout;
using std::endl;

int main() {
    cout << "Hello world" << endl;
    return 0;
}

But when you start working with STL, which always use the latest features whenever possible, then it starts to make a difference:

#include <iostream>
#include <string>
using std::string;

string getString(void) {
    string str("");
    for (int i = 0; i < 100000; i ++)
        str.append("A");
    return str;
}

int main() {
    std::cout << getString() << std::endl;
    return 0;
}

C++ - the language - doesn't make any performance guarantees beyond complexity limit O(*) for various operations - mostly containers, and general concept of "you don't pay for what you don't use".

There are no significant changes to this (except maybe the new container types that might be a better fit in a few places).

[edit] I forgot: move semantics can significantly speed up an existing code base without any changes. (However, if you've been squeezing cycles, you probably won't benefit.)

However (there's always one in C++) compilers usually have improved. Notable sources of significant performnance gains are new technologies like link-time code generation and automatic vectorization that have become much more widespead and refined, and better support for contemporary CPU architectures.

Against a compiler upgrade speaks only the risk and cost of research and testing.

Gut feel: Your seniors shy away from the risk and the change.

"C and c++03 [...] are [more] efficient compared to C++11 at micro level" is bull. Unless your seniors didn't bother to learn the newfangled stuff introduced 20 years ago.

Related