Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. May 11, 2004 #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)?
     
  2. jcsd
  3. May 11, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. May 11, 2004 #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: May 11, 2004
  5. May 13, 2004 #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?
     
  6. May 13, 2004 #5

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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)
     
  7. May 14, 2004 #6
    The MSVC++ 6 compiler only adds it if you have virtual functions, it appears.

    Thanks for the help, chroot and Hurkyl.
     
    Last edited: May 14, 2004
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Performance: OO vs Procedural (C++ vs C)
  1. Hda vs hdb vs hd* (Replies: 1)

  2. IE8 vs FF3 vs Chrome (Replies: 21)

Loading...