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

  • Context: C/C++ 
  • Thread starter Thread starter Dissident Dan
  • Start date Start date
  • Tags Tags
    performance
Click For Summary

Discussion Overview

The discussion centers on the performance differences between C and C++ programming languages, particularly in the context of specific features and constructs such as memory management, polymorphism, and input/output operations. Participants explore various factors that may influence performance, including compiler optimizations and the implications of object-oriented programming in C++.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • Some participants suggest that large C programs generally run faster than large C++ programs, but the extent of this difference and the contributing factors are uncertain.
  • One participant argues that the performance impact of using C versus C++ is heavily dependent on the compiler rather than the language itself.
  • Concerns are raised about the overhead introduced by vtables in C++ for polymorphism, with some participants noting that this is a significant runtime cost.
  • Benchmarks are mentioned that indicate stdio may outperform iostream, though this could be attributed to library implementation rather than the inherent nature of streams.
  • Participants discuss the performance implications of memory management functions like new/delete versus malloc()/free(), with one noting equal performance in tests.
  • Questions arise regarding the performance effects of multiple constructors, variable shadowing, and the overall object-oriented structure of programs.
  • There is a query about whether compiling C code as C++ in MSVC++ would yield performance advantages.
  • One participant inquires about the construction of vtables in relation to inheritance, questioning if they are created for all classes or only those that utilize inheritance.

Areas of Agreement / Disagreement

Participants express differing views on the performance implications of C versus C++, with no consensus reached on the overall impact of specific features or the relative speed of the two languages. The discussion remains unresolved regarding the performance effects of various constructs and the role of compilers.

Contextual Notes

Participants note that the performance differences may depend on specific implementations and optimizations by compilers, and there are unresolved questions about the impact of certain features on performance.

Dissident Dan
Messages
244
Reaction score
1
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:

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
4
Views
5K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 19 ·
Replies
19
Views
6K
Replies
6
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K