C vs C++

Aug 25, 2011 at 9:27am
Hi guys,

I know, there are threads out there covering parts of my question and this is a C++ forum, so I might have to change my nickname in a few...BUT:

I know about the benefits of C++, I'm far from expert but know my way around - stuff works for me, you know...right now I'm doing some heavy calculations with some C / C++ mix program. One run of it will take about a week on four cores, which are all close to 100% CPU (and I know I could do a lot better if I would have the time to optimise my data streams)...

My question now is: would the program possibly run faster (don't care for coding convenience) if written in plain C ??

Or in other words: how expensive (computational time) are the benefits of C++?



Aug 25, 2011 at 9:31am
My question now is: would the program possibly run faster (don't care for coding convenience) if written in plain C ??

No. Chances are that the C solution would be slower if you tried to recreate C++ features.

Or in other words: how expensive (computational time) are the benefits of C++?

Almost all of them are free. Which ones you mean exactly?
Aug 25, 2011 at 9:59am
C++ streams work a bit slowly, than C, for example.
Of course, an abstraction, connected with classes costs time.
But if you write on pure C using a good C++ compiler, there will be no difference, whether writing using a good C compiler.
Last edited on Aug 25, 2011 at 9:59am
Aug 25, 2011 at 10:04am
closed account (1vRz3TCk)
C versus C++ by Jakob Østergaard (February 24th, 2004)
http://unthought.net/c++/c_vs_c++.html
Aug 25, 2011 at 10:07am
Well I would have to get rid of my class structure (overgod classes wich are used as frontend controlling their servant classes, doing memory allocation and such) and replace it with basically straight forward c functions.

The question is: does it cost computational time to use these fancy and quite useful data types? Because that's what it is, a class is just a data type which can do stuff at its own, isn't it?
Aug 25, 2011 at 10:14am
@ Syuf and CodeMonkey

So basically if I write c and use a c++ compiler there will be no difference vs. using a c compiler...i thought so. But does "real" c++ (templated classes, heritage classes, classes AT ALL) impact on performance?

Other thing: is there a real difference between boost and pthread, well other than the former is a C++ library and the latter "just C". With some workaround both should do the trick even for memberfunctions ?!
Aug 25, 2011 at 10:18am
Well I would have to get rid of my class structure (overgod classes wich are used as frontend controlling their servant classes, doing memory allocation and such) and replace it with basically straight forward c functions.

Sounds like the design might be suboptimal. Generally, a class should have a specific, clearly outlined purpose.

The question is: does it cost computational time to use these fancy and quite useful data types?

Generally, no, although it depends on the context. At assembler level, a class is just a section of memory with a specific size. Accessing an element is reading or writing to a memory location at a specific offset. Calling a member function is the same as calling any other function, except with a hidden this parameter.

Because that's what it is, a class is just a data type which can do stuff at its own, isn't it?

I suppose you could say it like that.
Aug 25, 2011 at 10:22am
But does "real" c++ (templated classes, heritage classes, classes AT ALL) impact on performance?

Those are features that don't cost anything.
Code for templates is generated at compile-time and the layout of classes is also known at compile-time (even if they use inheritance).

Other thing: is there a real difference between boost and pthread, well other than the former is a C++ library and the latter "just C". With some workaround both should do the trick even for memberfunctions ?!

The difference is the ease of use and the proper integration in C++.
Internally, Boost.Thread also eventually ends up calling pthread functions.
Aug 25, 2011 at 10:27am
OK, that'll do for me...I don't do all that fancy programming. I just use classes to get some structure into my program and templates are THE solution for slim code. I'll stay with them, then ;)

Thanks guys...
Topic archived. No new replies allowed.