Steve Hebert's Development Blog

Sponsors

The Lounge

Wicked Cool Jobs

Currently Reading

My Amazon Wish List

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Raw memory allocation speed - c++ vs c#

I've had several conversations on OO languages in the past couple of months.  Going back to the migration from C to C++, developers became dependent on the 'new' operator and many times did so without understanding the consequences. In C++, 'new' = malloc() + non-inline function call (constructor). 

So I started wondering - what are the performance differences between the C# and C++ new operators.  To test this, I intentially created a small class that gets allocated over and over.  To better compare the two languages since C# uses garbage collection, I intentially avoided the destructor call in C++.  Since I cannot avoid the garbage collector, I decided that best case C# won't have time to call the gc and worst case C# will be penalized for it.

That said, I created the following code in C++. Keep in mind this is for the explicit purpose of isolating the function of allocating an object and calling a constructor.

C++ code

class OneInt

{

public:

   int i;

 

public:

   OneInt()

   {

      i=1;

   }

};

 

 

int main(int argc, _TCHAR* argv[])

{

   OneInt* oneInt = NULL;

 

   for( int i = 0;  i < 20000000; i++ )

      oneInt = new OneInt();   // intentional memory leak to avoid destructor call

 

 

   return 0;

}

C#  code

class OneInt

{

   public int i;

   public OneInt()

   {

      i=1;

   }

}

 

class Main

{

   static void Main(string[] args)

   {

      OneInt oneInt = null;

      for( int i = 0;  i < 20000000; i++ )

         oneInt = new OneInt();

   }

}

 

The results you ask?  On a Pentium 4 3.2 ghz box...

C++ averaged slightly more than 5 seconds to run.
C# averaged .15 seconds to run.

The +/- on both the C#/C++ code was less than .01 seconds, so I would consider that time to be TickCount resolution noise.

Having this type of code in a production environment is unrealistic - or at the very least, diplomas should be revoked.   I'm not sure how much you can mark up to the organization of the constructor call IL, but I suspect the allocation of system memory in chunks larger than the requested object is playing into this.  That task of preallocating memory and handling your own allocation pattern in C or C++ is very non-trivial work when the need arises.


Posted Fri, Mar 3 2006 12:10 AM by shebert

[Advertisement]

Comments

C# dev wrote re: Raw memory allocation speed - c++ vs c#
on Fri, Mar 3 2006 12:48 PM
The difference is in the memory allocator. The C++ heap algorithms are complex because they must be able to efficiently allocate and reclaim space without moving the allocated objects around in memory. C# uses a generational, moving collector. Allocating an object in C# involves a couple of additions and a call to the constructor, while in C++ it involves finding the best bucket of blocks for the requested size and updating the bucket pointers to take the next free block and then calling the constructor. All in all, C++ has to do more work when allocating memory on the heap.
Ross wrote re: Raw memory allocation speed - c++ vs c#
on Fri, Mar 3 2006 1:26 PM
It'd be interesting to see the MSIL generated by the C# version (I'd do it myself but I am on my Mac) to see what sort of optimisation the compiler made on that code.
Jason Haley wrote Interesting Finds
on Sat, Mar 4 2006 5:55 AM
Devlicio.us