8 years, 1 month ago.

c++ performance

How much performance sacrifice is caused by c++. I'm thinking of things like destructors and exceptions.

Are there ways of minimizing the slowing-down caused by c++ as opposed to c?

1 Answer

8 years, 1 month ago.

Exceptions are disabled in mbed, so no need to worry about that. Destructors are only called when you actually destruct something, they take a bit of memory but nothing significant.

Performance sacrifice is probably 90% that the basic C(++) stuff that is included (also because the mbed lib uses error messages, which uses printf, which forces it to include all the stdio stuff) consumes alot of memory. So on small devices if you have very little code, you can still require quite some memory. Of course if you actually start using printfs yourself, the extra cost is small.

Overall it depends mainly on how you do stuff. The Serial is not the fastest for example due to it using stdio functions. Or SPI cannot sent stuff on full speed using the mbed lib because it always needs to wait on the transaction to be finished, because it returns the read value. So if you only want to send, it still waits until the previous transaction is done, then it returns that value, and then the overhead of the next write action starts, meaning at high SPI frequencies you start losing performance. But this is no a C++ thing, but an mbed lib thing. There are some user libs that work around this for example.

Also for example https://developer.mbed.org/users/Sissors/code/FastIO/ (shameless self promotion) uses C++ features (templates) that result in performance that will be hard to much with plain C code.

Accepted Answer

"Performance sacrifice is probably 90% that the basic C(++) stuff that is included"

I'm not sure how to interpret that.

Regarding the calling of destructors, suppose a goto is done that exits six nested blocks each of which may have declared classes which might possibly have destructors. Doesn't the execution of the goto have to know which blocks are being exited and call destructors as necessary, meaning that even if there are no destructors, there's still the overhead.

I'm glad exceptions are disabled.

My past embedded experience is only with C and assembly language. Is c++ compiled code too nasty to look at at the assembly language level, and set breakpoints, etc.?

posted by Alfred Hume 19 Mar 2016

Regarding first one: Not that clear indeed. I meant that just the basic C++ stuff that is included with the mbed lib, such as stdio, takes alot of memory. It won't slow down your functions though.

Regarding destructors in your example: Well for sure even without explicit destructor, there is an implicit one, memory needs to be freed for example. There always are destructors for classes. In general if you have a function which really needs to go for high speed, don't construct/destruct object there. And especially not constructing/destructing from the heap (so with new and delete).

C++ compiled code will largely be similar as C for many things. But I never really looked more at it then at for example when it toggles an IO pin, what assembly is included exactly.

posted by Erik - 19 Mar 2016