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

  • C/C++
  • Thread starter Dissident Dan
  • Start date
  • Tags
    performance
In summary: The sizeof operator will give you the size of the vtable pointer, but not the size of the rest of the class.
  • #1
Dissident Dan
238
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
  • #2
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
 
  • #3
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:
  • #4
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?
 
  • #5
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)
 
  • #6
The MSVC++ 6 compiler only adds it if you have virtual functions, it appears.

Thanks for the help, chroot and Hurkyl.
 
Last edited:

1. What is the difference between Object-Oriented Programming (OOP) and Procedural Programming?

OOP is a programming paradigm that focuses on creating objects that have both data and behavior, while Procedural Programming is a programming paradigm that focuses on using procedures or functions to perform tasks. In OOP, objects interact with each other through methods and messaging, while in Procedural Programming, functions manipulate data directly.

2. Which is faster, OOP or Procedural Programming?

In general, OOP is slower than Procedural Programming because it involves additional overhead for creating and managing objects. However, the performance difference may not be significant in most cases and can vary depending on the specific implementation.

3. Why do some programmers prefer OOP over Procedural Programming?

OOP offers several benefits, including modularity, encapsulation, and code reusability. It also allows for easier maintenance and updates, as changes to one object do not affect other objects in the program. These advantages make OOP a popular choice for developing complex and large-scale software systems.

4. Can you use both OOP and Procedural Programming in the same program?

Yes, it is possible to use both programming paradigms in the same program. In fact, many programming languages, such as C++, support both OOP and Procedural Programming, allowing developers to choose the most suitable approach for different parts of their code.

5. Are there any limitations to using OOP or Procedural Programming?

Both OOP and Procedural Programming have their own limitations. OOP can be more complex and require a steeper learning curve for beginners. Procedural Programming may not be suitable for developing large and complex software systems. It is important for programmers to understand the strengths and limitations of each paradigm and choose the most appropriate one for their project.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
911
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
879
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
19
Views
2K
Back
Top