Reputation: 279
I am developing an application for embedded Linux (ARM). It will execute 500 times/sec, therefore speed is important. I would prefer to use C++ but I am afraid it will be slower than C even if I avoid fancy features like virtual functions. Is there a reason to use C or it's just as fine to write in C++?
Upvotes: 16
Views: 8458
Reputation: 3716
In C++ you have things like template metaprogramming that resolve in compile time several situations where C or any other procedural programming language would have to do in runtime.
I should say more. Template metaprogramming and some class inheritance tricks are really amazing. It can save you a lot of processing time that you'd spend otherwise by "ifing" and "switching".
This means that C++ can be actually faster than C if well conducted.
Obviously you can program "in C" using C++ and you'd have no penalty at all. If you're not too fond of C++ I'd advise you to do a "C on C++" or "C with C++ extensions" just to take vantage of C++ improvements, but the real advantage you'll have is by programming the C++ way. There you will see that C++ is, good part of times, or faster or cleaner than C or, at least as fast as.
Have no fear. Face C++. After the stdc++ (against libc) there will be almost no overhead in code size. If your application is from median to high in size, it will be diluted.
I use C++ from simple 8-bit ATmega to Marvell's ARM9, passing through AVR32 UC3 and Cortex-M3 and always find it profitable.
If you need specific advise in a given situation, feel free to ask.
Upvotes: 8
Reputation: 81
I am using ARM9 board for hardware control and I am using both C and C++ Application in 500 Mhz board. It is up to you to use the language and how you implement your logic to implement the functionality. Because I have not found any problem running my Application over the day controlling the hardware.
While writting your program, carefully select your variable, check it against extra instruction/loop, initialization. also use Gcc optimization flag while compilation.
I have not any problem running my Qt Application and C program on 500 Mhz ARM 9 board.
Upvotes: 1
Reputation: 40357
The real key to size and speed efficient code, embedded or otherwise, is for the programmer to fully understand the implications of his orher decisions.
To an extent, C++ provides more opportunities where something expensive can look deceptively innocent. Equivalent functionality to C++ features often requires more ink on the page, which may lead to a little more contemplation of its potential expense. But it's by no means an absolute - C (and its libraries) has the risk of deceptively innocent expense too.
Ultimately there is no substitute for understanding what you have asked for in each line of code.
Upvotes: 1
Reputation: 96109
C++ in general suffers no run time penalty over C - (except for a few things like RTTI).
Except in a few odd circumstances the compiler should be able to determine which virtual function to call at compile time and so add no overhead.
Edit: Ok with such a variety of compilers, CPUs, runtime libs, OSes there are some features of C++ that might create slower code, there are some features that might create faster code.
But can we all agree that C++ isn't automatically excluded from embedded use anymore ?
Upvotes: 18
Reputation: 40669
You can use C++ but be extra careful.
For size, keep a close eye on your linker map file. You can find it including tons of stuff you don't need, just from an innocent-looking declaration.
For speed, profile or random-pause often. It's super easy to do more new
s and delete
s than you really need, especially with container classes, and be really careful with things like iterators.
Often they're doing you un-asked-for favors.
You can step through the code at the assembly-language level to make sure it's only doing what you actually need, which should be about the same as the C code.
Upvotes: 2
Reputation: 91270
The main reason for choosing C over C++ is size of the compiled binary, which can be a real restriction for embedded systems.
On performance, there's no measurable difference, if you use the language right. You can write slow C code just as easily as slow C++, as long as you're aware of the under-the-hood mechanisms of what you're writing you should be fine with either.
Upvotes: 7
Reputation: 901
C++ is fine as long as you have enough RAM and flash in your embedded system. The C++ runtime library (libstdc++) is big, and comes in addition to the C standard library (libc) even if you use C++ only.
Upvotes: 3
Reputation: 15872
As long as you limit the features you use, you won't have much, if any, performance hit in C++ over C. The features you'll want to avoid include: exceptions, RTTI, and keep your class hierarchy as flat as possible (and use virtual functions sparingly).
Upvotes: 6