C/C++ Performance: OO vs Procedural (C++ vs C)

  • Thread starter Thread starter Dissident Dan
  • Start date Start date
  • Tags Tags
    performance
AI Thread Summary
The discussion centers on the performance differences between C and C++, particularly in large programs. It highlights that C programs may generally run faster due to factors like real-time checks for virtual functions and memory management differences. The use of C++ features like constructors and polymorphism introduces overhead, primarily through vtables, affecting runtime performance. Benchmarks indicate that standard I/O in C can outperform C++ streams, although this may depend on library implementations. The conversation concludes that while C++ offers advantages through modern compiler optimizations, the impact of object-oriented structures on performance remains a complex topic.
Dissident Dan
Messages
236
Reaction score
2
I'm wondering what the performance differences are between using C and C++. I think that it's quite clear that large C programs will generally run faster that large C++ programs, but how much so, and what factors have what effect?

For example:

I would assume minimal or no effect in the following areas:
-Using member functions as opposed to passing a struct as a pointer (and possibly using member function pointers)

moderate effect
-(non-inhereted) constructors

Large effect
-Real-time time checking for virtual functions (polymorphic method invocation)
-Real-time typecast type-checking (not sure if C++ does this. I know java does)
-Multiple constructors
-new/delete as opposed to malloc()/free()
-streams as opposed to FILE*, etc.

Not sure
-Multiple inheritance of functions
-Multiple inheritance of variables
-effects of different data/program organization
-Operator overloading (as opposed to function calls)
-Argument (parameter) pass by reference (ampersand) as opposed to pointer

This may actually provide an advantage to C++
-Compiler (C++ compilers are probably newer, having more optimizations)

What would be the performance effect of writing a .cpp file using only C code (so that a c++ compiler is used on the c code)?
 
Technology news on Phys.org
You cannot simply say that C is faster than C++. The language is not very important, the compiler is. All the high-level language does is describe a sequence of machine instructions in a more human-readable form. A good compiler will find the optimal sequence. Keep in mind that "the new operator" is not run on the hardware at all, and is not atomic.

The only major performance hits you'll take for C++ are the so-called vtables that the compiler constructs so that methods can be looked up at run-time -- polymorphism, for example, is handled by this mechanism. This is the only run-time overhead added by a C++ program; type checking, operator overloading and so on is still done only at compile-time.

- Warren
 
Thanks for the response.

I'm seen benchmarks that show stdio to be much faster than iostream, although this may be due to the implementation of the libraries, not the use of streams itself.

I just ran a little test benchmark, and new/delete and malloc()/free() were equal in time performance.

The typecasting that I mentioned was regarding polymorphism, which you said is handled by some things called vtables. Perhaps typechecking is not the correct term. Anyhoo, what is the performance difference (speed is a more important consideration than memory size) in polymorphism, not only run-time method look-up, but having multiple constructors, variable shadowing/hiding, and anything else I am neglecting to mention.

It also seems to me that while having classes have constructors can make programming simpler, having a function call with every data structure initiation could have adverse effects on performance.

I didn't really think that operator overloading would have much of an effect, since it seems to be just like defining a function using different syntax.

What about the OO structure of programs? Are there any ways to measure how this affects performance?

Would compiling C code as C++ in MSVC++ yield any performance advantages?
 
Last edited:
Does anyone know if vtables will be constructed even if your program does? What about if some classes have inheritance, and others, dont--will vtables be constructed for all classes or only those with inheritance?
 
You can use the sizeof operator to check; it includes the size of the vtable pointer. (Of course, this entails knowing how big the rest of your class is)
 
The MSVC++ 6 compiler only adds it if you have virtual functions, it appears.

Thanks for the help, chroot and Hurkyl.
 
Last edited:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top