Sign in to follow this  
johnnyBravo

How much faster is C compared to C++?

Recommended Posts

Exactly the same.

Most of today's C compiler really are C++ compilers. The parser may different, but the code generating engine remains the same.

If you were to implement, by hand, C++-equivalent constructs in C (e.g. polymorphism...) you would probably end up with a slower and kludgier solution.

Share this post


Link to post
Share on other sites
All things being equal, they should compile into pretty much the same instructions, hence the same speed.

Realistically, you should use whatever you create more maintainable, bug-free, and elegant code, as missing those will influence the speed of your program by orders of magnitude more than the speed difference between C and C++

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Just one more quick question, so C doesnt have classes but has structures?


C has struct and union, as well as enum.

Quote:
And anyone know where I can find some OpenGl tutorials written in C?


Nehe Tutorials on Gamedev.net
OpenGL.Org/
and in the Red Book (online)

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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++

and C is still slower than assembly!
if you want a realworld/game context example checkout the quake .net port:
How is the performance of the managed version?
Initially, the managed version was faster than the native version when the default processor optimization setting /G5 (Pentium) was used. Changing the optimization setting to /G7 (Pentium 4 and Above) created a native version that runs around 15% faster then the managed version.


Note, the assembly code was disabled for the native and managed versions so both versions are slower than the original Quake version

Share this post


Link to post
Share on other sites
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++

Share this post


Link to post
Share on other sites
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?

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. When using linux you could use the icc compiler tho since it briungs the c++ up to pretty much the same speed as c code. It really depends on what compiler you use, g++ has had some issues and hopefully they will be/are solved ;) Until then I'm gonna stick to C(gcc) for time critical low level stuff. I doubt you are using linux tho so just go with whatever feels right.

Note: all of the above is MHO so don't get too excited.

- Damage Incorporated.

Share this post


Link to post
Share on other sites
c speed==c++ speed

The only way c++ would be slower is if you use things like polymorphism. And that is not due to the language but the design. If you design your program right however using polymorphism (etc.) the speed diffrence is very very marginal.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this