C++ VS C

Started by
18 comments, last by v0dKA 19 years, 10 months ago
quote:Original post by Etnu
C has a very slight speed advantage in the sense that your functions won''t be encapsulated within a class, which removes the accessor overhead.

This overhead is extremely minimal, and if you consider it a reason not to use C++, you have very poor criteria for language selection.

I know that you''re suggesting one discard the overhead, but I always found this ''overhead'' somewhat curious. Assuming non-virtual functions, I don''t see what overhead there is to speak of other than the implicit passing of the this pointer ... but the equivalent C function would probably take a struct pointer as an argument, so there''s no real difference here.

Assuming virtual functions, well, that''s a different kettle of fish ... but again, in order to write equivalent C code (presumably you do not use virtual functions if you do not need the functionality), you will have to implement the same functionality, by, say, a switch statement (slow) or by tables of function pointers (where again, you''re back to what C++ does for you) - again, I fail to see the significant difference.
Advertisement
quote:Original post by Miserable
I know that you''re suggesting one discard the overhead, but I always found this ''overhead'' somewhat curious. Assuming non-virtual functions, I don''t see what overhead there is to speak of other than the implicit passing of the this pointer ... but the equivalent C function would probably take a struct pointer as an argument, so there''s no real difference here.

Assuming virtual functions, well, that''s a different kettle of fish ... but again, in order to write equivalent C code (presumably you do not use virtual functions if you do not need the functionality), you will have to implement the same functionality, by, say, a switch statement (slow) or by tables of function pointers (where again, you''re back to what C++ does for you) - again, I fail to see the significant difference.


Depends on what you mean by "equivalent". If you mean "does the same thing under the hood", then, of course, there''s no difference. They''re doing the same job in the same way. If you mean "takes the same inputs and gives the same outputs" then it''s entirely possible that a C programmer will use a different solution under the hood than the C++ programmer. Which has less overhead depends on the specific instance.

Also, I was unaware that switch statements are slow. I''m not challenging what you''re saying there. Instead, I''d like to be enlightened. Anyone have numbers and a rationale?
quote:
if you absolutely need to micro-optimize, then you can always drop to a more C-like style in C++.


If you need to micro-optimize, you should be using _asm most likely ^_~

Miserable: Yes, I''m aware that the overhead of passing the this pointer and whatnot is minimal. That was my original point :-p. Some C programmers insist that it''s a big deal, when anyone with common sense knows that it''s not.

Way - yes, I meant they should know both and be able to program in both.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

quote:Original post by Way Walker
Also, I was unaware that switch statements are slow. I''m not challenging what you''re saying there. Instead, I''d like to be enlightened. Anyone have numbers and a rationale?

Well, depending on the data, a switch statement might degenerate into an if-else chain. With the right data it can, of course, be optimised into a jump table by a sufficiently clever compiler, but there''s no guarantee. For something as ubiquitous as a vtable - where fast lookups are obviously essential - a linear if-else chain could be disastrous.
quote:Original post by Miserable
Well, depending on the data, a switch statement might degenerate into an if-else chain. With the right data it can, of course, be optimised into a jump table by a sufficiently clever compiler, but there''s no guarantee. For something as ubiquitous as a vtable - where fast lookups are obviously essential - a linear if-else chain could be disastrous.


Not that I know anything about assembly or compiler optimizations, but I''ve heard that the compiler will probably use a jump table if the values are roughly sequential. This is likely the case when the values are from an enum (which mine tend to be) and would probably be the case in this instance. By "no guarantee", do you mean it''s an uncommon optimization (i.e. most compilers will not do it) or that the standard is silent on the issue?
quote:Original post by Direct
if your doing windows programming, whether you do C or C++ you are going to use the same compiler (VC++) because it optimizes the best...


The Intel compiler beats the shit out of the Microsoft compiler any day.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Aldacron
In my opinion, C++ has the most bloated specification of any other language out there, and it''s biggest failing is that it caters to several different programming paradigms/styles (something others see as its greatest strength).


You haven''t seen Common Lisp have you. It makes C++ look small. :-)
My name is my sig.
Well, I use C++ to set up DirectX and a few classes of my own to recycle code from project to project - but the actual game itself is done in C. I prefer structures to classes - simplicity to complexity etc...

What Andre LaMothe is trying to get across to the reader is to keep your code simple. Don''t use linked lists, classes etc just for the sake of it - your game will get complex soon enough, so don''t throw fuel on the fire IF you can help it...

My advice to you is to decide what C or C++ function/features are the best for your needs. Using a function/whatever only because its C++ and not C isn''t wisdom.

Although many will grind me up for such a practice - I prefer printf, scanf etc to Cin or Cout! But thats because they fit into my current project quite nicely...though the next project, I may decide to use Cin/Cout - it all depends on my needs at the time.

Do you see?

Languages; C, Java. Platforms: Android, Oculus Go, ZX Spectrum, Megadrive.

Website: Mega-Gen Garage

There is no real difference between c & c++ as far as performance goes. If there is you should contact the company/whatever that created the compiler and complain. Do a google and search for Bjarne Stroustrup and c++ performance, read what he has to say about the matter. C/C++/c# blaha topics are utterly useless, use what you think feels best for you and don''t listen to what anyone has to say about it.

No no no no! :)
quote:Original post by Etnu
Miserable: Yes, I''m aware that the overhead of passing the this pointer and whatnot is minimal. That was my original point :-p. Some C programmers insist that it''s a big deal, when anyone with common sense knows that it''s not.

I know - my point is that I don''t see why there should be any overhead, minimal or not, and it''s a bit of a pet peeve of mine, though usually it comes from rabid C addicts (and without the adjective).

quote:Original post by Way Walker
Not that I know anything about assembly or compiler optimizations, but I''ve heard that the compiler will probably use a jump table if the values are roughly sequential. This is likely the case when the values are from an enum (which mine tend to be) and would probably be the case in this instance. By "no guarantee", do you mean it''s an uncommon optimization (i.e. most compilers will not do it) or that the standard is silent on the issue?

It certainly makes sense for the compiler to do so, but without studying assembly output it is hard to know if it''s happening. You said yourseif you believe it will do so if the values are "roughly sequential" - well, what is sequential enough? Also note that this obviously depends on the quality of your compiler; I was deliberately vague on this because I don''t know what compilers do or do not generate jump tables for switches, or how aggressively.

As for the standard, no, it doesn''t specify how a case is to be selected, only how the program flow is affected.

This topic is closed to new replies.

Advertisement