mathsjery
JF-Expert Member
- Sep 26, 2015
- 2,208
- 1,755
Hello Mhariri , change the thread title to
If C is faster than C++, then why are companies like SpaceX (and autonomous vehicle companies) using C++ when they need the efficiency Code written in C is not inherently faster than code written in C++. C++ can be used to write code that is just as efficient as C code, assuming the C++ compiler has equivalent optimization settings and options. There is nothing magical about C that makes the generated code automatically faster. While C++ is a much larger, more complex programming language than C, you only pay for what you actually use, in terms of performance.
There is no “performance tax” overhead just for using C++. Keep in mind that inefficient, bloated code can be written in any programming language, including C or C++ or even assembly language. Both C and C++ require a level of knowledge and discipline to use them properly, along with the use of appropriate algorithms and data structures, to produce well-performing code. Another thing to keep in mind is the maturity and quality of the development tools. Not all compilers are created equally — some provide more advanced optimization than others, etc.
The same C source code can potentially result in very different generated code performance, depending on the compiler and on the options being used during compilation. The same goes for C++ and any other language implemented as a compiler. Programming languages themselves don’t have an inherent speed or an inherent relative speed.
For larger projects, worked on by larger teams, C++ can provide some distinct advantages over C, and many of these advantages can be used without sacrificing any performance. In some cases, a C++ implementation can result in better performance than a C implementation. C++ is a perfectly valid choice for large, complex projects which require high performance, and projects which have real-time performance requirements.
Now, some library functions/methods might be designed or implemented differently, leading to significant differences in execution speed or code size or both, when attempting to accomplish the same thing in the two different languages. For example, consider sorting an array. In C, you might choose to use the qsort standard library function, and define a comparison callback function. In C++ you might choose to use a sort function with iterators, provided by the C++ Standard Library.
Because of the qsort call overhead (calling your comparison function on each comparison) and the logic required to return the appropriate integer value indicating comparison status, and because the C++ sort function is likely implemented to inline all comparisons, the C++ sort approach can be significantly faster than the C qsort approach. However, if you take the time to write your own dedicated sort function in C, for the specific type of data you’re sorting, you can end up with code that is just as efficient as the code provided by the C++ Standard Library.
The amount of work required to accomplish this is higher in C, but you can still achieve the same execution efficiency. Conversely, if you perform sorting in C++ using the C qsort library function, the performance of the sort will be effectively the same. While the libraries associated with the two languages may take very different approaches and yield very different performance results, the language itself is not inherently faster or slower than the other.
You can always alter your use of library functions, implement things yourself, make use of macros, templates, inlining, etc. to achieve your performance goals. Be careful about making gross generalizations. C is not inherently faster than C++, and C++ is not inherently faster than C.
If C is faster than C++, then why are companies like SpaceX (and autonomous vehicle companies) using C++ when they need the efficiency Code written in C is not inherently faster than code written in C++. C++ can be used to write code that is just as efficient as C code, assuming the C++ compiler has equivalent optimization settings and options. There is nothing magical about C that makes the generated code automatically faster. While C++ is a much larger, more complex programming language than C, you only pay for what you actually use, in terms of performance.
There is no “performance tax” overhead just for using C++. Keep in mind that inefficient, bloated code can be written in any programming language, including C or C++ or even assembly language. Both C and C++ require a level of knowledge and discipline to use them properly, along with the use of appropriate algorithms and data structures, to produce well-performing code. Another thing to keep in mind is the maturity and quality of the development tools. Not all compilers are created equally — some provide more advanced optimization than others, etc.
The same C source code can potentially result in very different generated code performance, depending on the compiler and on the options being used during compilation. The same goes for C++ and any other language implemented as a compiler. Programming languages themselves don’t have an inherent speed or an inherent relative speed.
For larger projects, worked on by larger teams, C++ can provide some distinct advantages over C, and many of these advantages can be used without sacrificing any performance. In some cases, a C++ implementation can result in better performance than a C implementation. C++ is a perfectly valid choice for large, complex projects which require high performance, and projects which have real-time performance requirements.
Now, some library functions/methods might be designed or implemented differently, leading to significant differences in execution speed or code size or both, when attempting to accomplish the same thing in the two different languages. For example, consider sorting an array. In C, you might choose to use the qsort standard library function, and define a comparison callback function. In C++ you might choose to use a sort function with iterators, provided by the C++ Standard Library.
Because of the qsort call overhead (calling your comparison function on each comparison) and the logic required to return the appropriate integer value indicating comparison status, and because the C++ sort function is likely implemented to inline all comparisons, the C++ sort approach can be significantly faster than the C qsort approach. However, if you take the time to write your own dedicated sort function in C, for the specific type of data you’re sorting, you can end up with code that is just as efficient as the code provided by the C++ Standard Library.
The amount of work required to accomplish this is higher in C, but you can still achieve the same execution efficiency. Conversely, if you perform sorting in C++ using the C qsort library function, the performance of the sort will be effectively the same. While the libraries associated with the two languages may take very different approaches and yield very different performance results, the language itself is not inherently faster or slower than the other.
You can always alter your use of library functions, implement things yourself, make use of macros, templates, inlining, etc. to achieve your performance goals. Be careful about making gross generalizations. C is not inherently faster than C++, and C++ is not inherently faster than C.