How much faster is C compared to C++?

Started by
9 comments, last by Qw3r7yU10p! 19 years, 7 months ago
Quote:Original post by Damage_Incorporated
Quote:Original post by petewood
Quote:Original post by johnnyBravo
Hi, I was wondering if anyone knew averagely, how much faster C is, compared to C++.


C is slower than C++


Really what do you base that on? I'd really like to know just pulled it out of your arse or?

Heh. I posted that for two reaons. One was that the original question only wanted to know how much faster c was than c++ and didn't ask in what ways c was faster than c++ and what ways c++ was faster than c. Secondly I posted it because there are specific measurable uses of c++ which are faster than c and reasons why c++ will produce code which is as good as c. So I'm glad you asked.

Quote:
Anyway from my experience with gcc/g++ c++ seems to have a slightly higher memory usage than c, and is also slightly slower when you fully utilize such features as polymorphism etc.


If you are using polymorphism then you are benefiting from creating a design which isn't natively supported by c. If you wanted an equivalent mechanism in c you'd have to code it by hand and you'd get the same performance hit (of looking up function pointers in a table). Actually though, if I coded it it's likely it would be slower and buggier than doing it in c++. It would also take me a lot longer to write. The compiler will be far more able to optimise the solution than I would.

A different, more concrete example of C++ being faster than C is in using the std::sort algorithm as opposed to qsort. std::sort is a templated function which takes the first and last iterator of a sequence of data and a function like object for doing the comparisons between each piece of data. As it is possible to use a function object rather than a function pointer (which is all that qsort can use), the compiler is able to inline the comparison function rather than calling it through a pointer. Write some tests and you'll see it's a lot faster. See this website if you're not interested enough to write tests for yourself.

The type information, templates and operator overloading available in c++ mean you can write very efficient and expressive code which wouldn't be possible in c. See for example the blitz library, Expression Templates, Matrix Template Library.

This topic is closed to new replies.

Advertisement