Select Page

C++: Fast and small

How much does a voice feature cost?

Author: Andreas Fertig, Philips Medical Systems Böblingen

Contribution – Embedded Software Engineering Congress 2017

With the C++11 standard and its successors, C++ developers can benefit from exciting new features of the language. Features always come at a cost, either in terms of speed or code size. To effectively use C++ according to the motto "you pay only for what you use," it's crucial for developers to understand the specific costs associated with each feature.

The C++ programming language is known for being both object-oriented and efficient. Its motto is "you pay only for what you use." We can use language features in our code and thus pay only for their use. At the same time, we benefit from potential advantages. If we don't use certain features, the costs associated with them are eliminated. We can therefore control costs to a certain extent. These costs can be attributed to increased memory consumption (RAM or ROM) or runtime. A combination of both is also possible.

Temporary objects are one example. The language helps us perform conversions from one type to another. A conversion can also be expressed more clearly, thus eliminating the costs of creating and destroying the temporary object.

We had many years, from 1999 to 2011, to acquire knowledge about the costs of language features. With the new standard C++11 and its successors, the language has undergone a multitude of innovations. These cost changes are accompanied by advantages that are worth considering when implementing a feature.

Let's look at car. With this keyword, we leave the determination of a variable's data type to the compiler. Here, we simply tap into the compiler's existing internal knowledge. We ask it to transfer the type of the expression on the right-hand side to the left-hand side. Since C++ is not a dynamic language, the type information is fixed at compile time. There is no runtime penalty. car We can reduce lengthy template expressions and let the compiler do the writing. In some cases, this helps us. car, because it guarantees that the same type is on the left and right. This prevents careless mistakes such as const Avoid going right but not left, which can lead to a temporary object.

Next to car C++ was extended with so-called range-based for loops, also known as foreach loops in other languages. These help us write clearer and more concise code. Before C++11, a loop to iterate over a vector could be written as follows:

std::vector numbers{1, 2, 3, 5};

for(auto it = numbers.begin(); it != numbers.end(); ++it)

{

  printf("%d", *it);

}

Here it is necessary to provide details of the type, such as... begin and end to know. Apart from that, the code in the head of the for loop is neither surprising nor challenging for even inexperienced developers. The code is rather tedious to write and read. Using C++11 tools, we again leave the filling in of the code to the compiler and reduce our writing to the essentials. In conjunction with car We can reduce this essential aspect even further. The same loop looks like this in C++11:

std::vector numbers{1, 2, 3, 5};

for(auto it : numbers)

{

  printf("%d", it);

}

It's significantly more compact to write and read. But what happens behind the scenes to achieve the same effect as before? And how efficient is what the compiler does for us at this point?

This is C++, and the standardization committee is always striving to make even the newest features efficient. This is also the case here. The standard [1] defines under [stmt.ranged] what a range-based for loop looks like:

auto && __range = range-init;

for ( auto __begin = begin-expr,

           __end = end-expr;

      __begin != __end;

      ++__begin ) {

  for-range-declaration = *__begin;

  statement

}

At this point, the standard uses internal tools. car to determine the range type. Otherwise, a range-based for loop relies on either three functions in a class that provide the start and end points, and an operator for comparing inequality. The rest is a standard for loop.

A range-based for loop offers a bit more than just simplified syntax. Firstly, the standard uses the pre-increment instead of the also possible post-increment. This avoids the temporary object that occurs with a post-increment. Furthermore, the variable __end A hard cache of the loop termination condition is built up. This can result in a speed advantage, as the condition does not need to be re-entered in every loop iteration. end() is called up. Of course, this does not allow any tricks to be performed within end() implement more.

Overall, it can be said that C++ remains true to its principles even with the new standards. Only features that are actually used incur costs. The standard ensures that each feature delivers a real benefit. This also applies to features not discussed here, such as lambdas or structured bindings. Nevertheless, it's worthwhile to stay up-to-date regarding costs.

Bibliography and list of sources

[1] Working Draft, Standard for Programming Language C++ (N4687)

Download the article as a PDF


Implementation – our training & coaching

Do you want to bring yourself up to date with the latest technology?

Then find out more here MircoConsult offers training courses/seminars/workshops and individual coaching on the topic of implementation/embedded and real-time software development.

Training & coaching on the other topics in our portfolio can be found here. here.


Implementation – Expertise

Valuable expertise in the field of implementation/embedded and real-time software development is available. here Available for you to download free of charge.

To the specialist information

You can find expertise on other topics in our portfolio here. here.

MicroConsult Newsletter

With the MicroConsult newsletter, you'll stay on the pulse of the embedded world. Look forward to proven practical knowledge, real professional tips, and current events – directly from our experts for your project success.

Subscribe now!

Published by

weissblau media

weissblau media