Wednesday, August 29, 2007

Assembly Benchmarks for OOC and C++

I checked some benchmarks for inheritance, here are the two files:
 /* begin test1.c */

#include < stdlib.h >
#include < stdio.h >

typedef struct {
 void (*method)();
} abstract_class;

void base_method()
{
 printf("In base::func()\n");
}

void d1_method()
{
 printf("In d1::func()\n");
}

void d2_method()
{
 printf("In d2::func()\n");
}

int main(void)
{
 abstract_class a, b, c;
 a.method = base_method;
 b.method = d1_method;
 c.method = d2_method;
 
 a.method();
 b.method();
 c.method();
 
 return 0;
}
and the C++ program:
 /* test.cpp program */

#include < iostream >
#include < stdlib.h >
#include < stdio.h >
using namespace std;

class base                          //Base Class
{
 public:
   virtual void func()
   {
     printf("In base::func()\n");
   }
};

class d1:public base                       // Derived Class 1
{
 public:
   void func()
   {
    printf("In d1::func()\n");
   }
};

class d2:public d1     // Derived Class 2
{
 public:
   void func()
   {
    printf("In d2::func()\n");
   }
};

int main()
{
  base b;
  d1 d;
  d2 e;

  b.func();
  d.func();
  e.func();

  return 0;
}
The resulting assembly files (from typing $ g++ -S test.cpp and $ gcc -S test1.c). The C++ program generated 533 lines of assembly, whereas the C program generated 74 lines of assembly...the C program was 720.27027027% more efficient. The vtable for C++ makes it inefficient...though you wouldn't notice the how high powered hardware of today. Note I used the GNU Compiler Collection:
 $ gcc --version
gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Appendix

(Revised appendix made on 19 September 2007) I recently reconsidered this as sort of unfair since I was using two different front ends for the GCC. I suppose this shows that the C front end is far far more efficient than the C++ one, which is ultimately all I proven. But upon reconsideration, I tried using the G++ compiler to compile the C program, and I found out that by doing this the number of lines of code in the resulting assembly is 216 lines of code. This C implementation is still 246.759259259% more efficient, but it was misleading how more efficient the C program was. I'm torn still as to which statistic to use, because if one were compiling a C program one would use the C Compiler...or at least I would. The moral of the story, I guess, is that object oriented C is 2.5 to 7.2 times more efficient than C++...depending on how you compile it. If you want to be efficient, use the C Compiler.

2 comments:

Patrick Aupperle said...

I know this is old, but google pulled it up. One thing I couldn't help but notice, was that you #include both c libraries in the c++ version. I am not experienced, but I really can't see this being necessary. I don't really know C or C++, but I think that it would probably be better to rewrite the C++ version so as not to use the C libraries. This might bring down the number of lines in the assembly file.

Just thoughts from someone who knows little to nothing about either C or C++, please correct me if anything in my post is inaccurate.

Patrick Aupperle said...
This comment has been removed by the author.