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?
Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.
Assembly is almost pure binary so it is without bias the fastest language. C is the fastest because it's the speed of light, and relativity?
The same code in C and C++ should usually run at exactly the same speed, the exception being code that has different semantics due to different aliasing rules, etc. The difference is between C idioms and C++ idioms.
Both C and C++ are compiled to machine code, therefore neither is "faster" than the other. Having said that, C++, due to its higher-level abstractions, gives the programmer much more ability to write inefficient code than in C.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With