Is C notably faster than C++ [closed]
As far as I understand all scripting languages and core scientific programs are usually written in C; this renders the implementation messy yet in a way straight to the point.
I understand these people would like to max their performance but is there a real difference between using C strings and C structures to using C++ classes; C++ seems to work the same way, apart from virtual functions, it stores a class function once and every instance of that class calls that one function.
What makes C faster and is it a notable difference in a project such as python or sqlite who have to be the fastest?
Solution 1:
C++ is often used for scientific programs. The popularity of C may be waning in that domain. Fortran remains popular as a "low-level" language.
In C++, "you only pay for what you use." So there is nothing that would make it any slower than C. In particular for scientific programs, expression templates make it possible to perform some custom optimization using the template engine to process program semantics.
The reason C is preferred for projects such as Python is that it tends to be less confusing to read, so a large codebase will be more accessible to a larger pool of contributors.
SQLite has a requirement for small executable code size, where C does have a slight edge. Judicious use of C++ still allows use in embedded applications, but it is less popular due to fear that unwanted language features will creep in.
Solution 2:
I don't think that the reason is so much related to performance as it is to interoperability. The C++ language is more complex than the C language, but from a performance point of view there shouldn't be a notable difference in either way. Some C++ constructs are faster than the C equivalent (std::sort
is faster than qsort
) and there are probably good examples of the other way around.
EDIT: On the interoperability side...
Basically, the C++ standard does not define some of the things that might be needed for easy interoperability between binaries created with different compilers/versions. The most notable issue here would be the naming convention for the symbols in the binary. In C, the language defines a single mapping from each symbol in code to the binary symbol name. A function called my_function
will create a symbol in the binary called my_function
. On the other hand, and due to features like function overloading, the names of C++ functions have to be mangled (translated into different function symbols in the binary, encoding the types of the arguments and return types), and the standard does not define how the mangling is performed. That in turn means that the same function in C++ can be compiled to different symbols depending on the compiler (unless extern "C"
is used to force C interoperability for those functions in C++).
At the end of the day, the interface between the scripting language and the native code would have to be a C interface anyway, even if the details of how it is implemented internally could be C/C++/any other native language.
(I am intentionally not wanting to enter into a flame war of language prefences, C++ is really powerful, but it is also a bit scary as it is a much more complex language than C, and some things that look simple might have an impact on performance)
Solution 3:
As Bjarne mentioned in [D&E] the effectiveness is one of the main goals of C++. So C++ is slower only when programmer uses its "extra" functions like virtual functions you mentioned, rtt information etc
So I think it is more of psychological reasons - C is used as it doesn't allow "slow" C++ features.