This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What is the performance difference in writing C and C++ code for ARM Cortex M7 mcu ?

Hello All,

             So far, I have been writing code for ARM Cortex M7 (STM32F7691) using C language. But I am planning to write more structured, OOPS style coding for my next application. And so, I am thinking about using C++ instead of C. Please let me know the following,

1. What will be the performance difference and also the final code size (excluding the programmer's skills )?

2. Does arm cortex m7 has good c++ compiler?

3. Specifically, Does OOPS methodology causes performance overheads?

Thank you.

  • Hi,

    1) Overhead depends on the functionalities you want to use in C++(exceptions..). Be careful with template/generic programming, as it adds a lot of code in your back. . Some libraries are HUGE and slow(ish) ,like Boost. You will find so much info searching for c vs c++.

    OOP for C has been done but not as powerful/clean as C++ IMHO.

    2) Kind of. Gcc is old on ARM. But manages to create ok code.

    3) Memory proximity, indirect pointers (virtual classes) can cause performance overhead. There will be space overhead too in the resulting binary in C++ because more information has to be carried.

    So, if your board can take it and you want it, go for it. Otherwise, stick with C.. or your own language :)

  • One cannot answer such question in general. There are surely some traps with C++, but in the end it all depends on you. You can write good performing code in C++ and horrible slow code in C.

  • The performance difference depends on the assembly code generated.

    You'll have to get some basic ARM assembly understanding and check the code during the development process to identify potential performances losses due to the compiler doing things you never intended to do. Having an idea on what assembly code a C/C++ should generate, compare it and evaluate can clearly help you identify performances issues due to the language itself very quickly.

    Profiling and benchmarking the generated algorithms can also help you identify performances issues. If you can implement the same algorithms with two different languages, you might be able to clearly figure out which one you should use, performance wise.

    C++ has a lot of features making the compilers extremely conservative at times. Here are some hints that you can try to follow : http://www.agner.org/optimize/optimizing_cpp.pdf

    I remember that managed some webinars about using C++ for embedded systems projects.

  • I agree completely with the 3 above answers.

    In some cases you might be able to use assembly language as an alternative to OOP.

    Yes, this might sound strange and it's only in some cases and it's not often.

    In general, C++ is avoided on microcontrollers due to their small amount of flash memory and small amount of available RAM.

    If you have a Cortex-M7 (or a Cortex-M4 based STM32F4), then I am quite convinced that you would have plenty of both Flash memory and on-chip SRAM to use C++.

    I personally never used C++ on a microcontroller, however I'm not completely against it (I just hate overhead and wasted clock-cycles, and C++ in general is sluggish compared to C).

    -However, in many cases it's possible to write your code / objects "lightweight" (eg. almost C-like) and keep a good overview, which helps you to keep the code short or clean or quick or any combination of those.

    That might help you generate code in a short time, which would outperform a similar attempt in C; it might also help you avoid too many bugs if your code is easier to overview.

    If you're interested in C++ on Cortex-M, you might want to have a look at mbed.

    I've never tried or used mbed myself, but I think it's a great idea and certainly worth mentioning. ;)