Performance comparison between C# and C++ 
Author Message
 Performance comparison between C# and C++

Hi All,

The concept behind the dot net framework is really great. It is nice to
finally be able to mix languages. However there is a penalty to pay for
this. The rift between C++ and other languages as far as speed is concerned
still continues. If you execute the code below you will notice that the C++
code is 4 times faster than the similar one in C#. If the underlying
framework is the same then why this difference?

What gives folks?

 My decision to uses C++ hinges on this performance issue. I would love to
use C# or VB.Net as it is far simpler to code but if it is going to hamper
performance.....

Thanks
Prady

C# Code

class Image
{
 public long m_lStatus;
 public Image()
 {
  m_lStatus = 0;
 }

Quote:
}

class SomeObj
{
 public Image [] img;
 public SomeObj()
 {
  img = null;
 }

Quote:
}

class Class1
{
 [STAThread]
 static void Main(string[] args)
 {
  DateTime dt = DateTime.Now;
  Random rnd = new Random();
  SomeObj [] arr = new SomeObj[100];

  for(int i = 0; i < 100; i++)
  {
   arr[i] = new SomeObj();
   arr[i].img = (Image [])new Image[6000];
   for(int j = 0; j < 6000; j++)
   {
    arr[i].img[j] = new Image();
    arr[i].img[j].m_lStatus = rnd.Next(int.MaxValue);
   }
  }
  TimeSpan diff = DateTime.Now - dt;
  Console.WriteLine(diff.TotalMilliseconds);
 }

Quote:
}

C++ Code:
class Image
{
public:
 long m_lStatus;
 Image()
 {
  m_lStatus = 0;
 }
Quote:
};

class SomeObj
{
public:
 Image *img;
 SomeObj()
 {
  img = NULL;
 }

Quote:
};

int _tmain()
{
 SomeObj *arr;
 Random *rnd =  new Random();

 DateTime dt = DateTime::Now;
 arr = new SomeObj[100];
 for(int i = 0; i < 100; i++)
 {
  arr[i].img = new Image[6000];
  for(int j = 0; j < 6000; j++)
  {
   arr[i].img[j].m_lStatus = rnd->Next(65530);
  }
 }
 TimeSpan t = DateTime::Now - dt;
 Console::WriteLine(t.TotalMilliseconds.ToString());
 return 0;

Quote:
}



Tue, 18 Oct 2005 10:39:40 GMT  
 Performance comparison between C# and C++

Quote:
> What gives folks?

Your benchmark code is doing different things in each case.  The C++ code
uses stack allocation, while your C# code uses heap allocation, which
accounts for nearly all the difference in timing you're seeing.  Change the
C# code to structs so they are allocated inline with the array (which
eliminates the need for the new per loop in your code), and the results are
pretty similar.

Nick Wienholt, MVP
Sydney Deep .NET User Group www.sdnug.org



Tue, 18 Oct 2005 11:07:10 GMT  
 Performance comparison between C# and C++

Quote:
> Hi All,

> The concept behind the dot net framework is really great. It is nice to
> finally be able to mix languages. However there is a penalty to pay for
> this. The rift between C++ and other languages as far as speed is
concerned
> still continues. If you execute the code below you will notice that the
C++
> code is 4 times faster than the similar one in C#. If the underlying
> framework is the same then why this difference?

> What gives folks?

>  My decision to uses C++ hinges on this performance issue. I would love to
> use C# or VB.Net as it is far simpler to code but if it is going to hamper
> performance.....

This code allocates 600,000 objects and does nothing with them.  That is,
it's purely a benchmark for object allocation, and tells you little or
nothing about the performance of a real-life application.


Tue, 18 Oct 2005 13:14:19 GMT  
 Performance comparison between C# and C++
This article might help :-

http://www.codeproject.com/dotnet/PrimeNumbersProjects.asp

Regards,
Nish [VC++ MVP]


Quote:
> Hi All,

> The concept behind the dot net framework is really great. It is nice to
> finally be able to mix languages. However there is a penalty to pay for
> this. The rift between C++ and other languages as far as speed is
concerned
> still continues. If you execute the code below you will notice that the
C++
> code is 4 times faster than the similar one in C#. If the underlying
> framework is the same then why this difference?

> What gives folks?

>  My decision to uses C++ hinges on this performance issue. I would love to
> use C# or VB.Net as it is far simpler to code but if it is going to hamper
> performance.....

> Thanks
> Prady

> C# Code

> class Image
> {
>  public long m_lStatus;
>  public Image()
>  {
>   m_lStatus = 0;
>  }
> }

> class SomeObj
> {
>  public Image [] img;
>  public SomeObj()
>  {
>   img = null;
>  }
> }

> class Class1
> {
>  [STAThread]
>  static void Main(string[] args)
>  {
>   DateTime dt = DateTime.Now;
>   Random rnd = new Random();
>   SomeObj [] arr = new SomeObj[100];

>   for(int i = 0; i < 100; i++)
>   {
>    arr[i] = new SomeObj();
>    arr[i].img = (Image [])new Image[6000];
>    for(int j = 0; j < 6000; j++)
>    {
>     arr[i].img[j] = new Image();
>     arr[i].img[j].m_lStatus = rnd.Next(int.MaxValue);
>    }
>   }
>   TimeSpan diff = DateTime.Now - dt;
>   Console.WriteLine(diff.TotalMilliseconds);
>  }
> }

> C++ Code:
> class Image
> {
> public:
>  long m_lStatus;
>  Image()
>  {
>   m_lStatus = 0;
>  }
> };
> class SomeObj
> {
> public:
>  Image *img;
>  SomeObj()
>  {
>   img = NULL;
>  }
> };

> int _tmain()
> {
>  SomeObj *arr;
>  Random *rnd =  new Random();

>  DateTime dt = DateTime::Now;
>  arr = new SomeObj[100];
>  for(int i = 0; i < 100; i++)
>  {
>   arr[i].img = new Image[6000];
>   for(int j = 0; j < 6000; j++)
>   {
>    arr[i].img[j].m_lStatus = rnd->Next(65530);
>   }
>  }
>  TimeSpan t = DateTime::Now - dt;
>  Console::WriteLine(t.TotalMilliseconds.ToString());
>  return 0;
> }



Tue, 18 Oct 2005 13:44:30 GMT  
 Performance comparison between C# and C++
As has already been pointed out, your comparison is severly flawed.
In the C++ code the Image and SomeObj classes are not garbage
collected, which means they will behave like normal C++ classes.
The C++ code therefore is incorrect. It leaks memory, since it
allocates objects on the C++ heap and never frees them!
Compiling C++ code to IL instead of native instructions does not make
it fully "managed". In order to make the comparison fair, the Image
and SomeObj classes must be marked with the __gc modifier and
the code changed accordingly (see below).

There are also a couple of other inconsistencies in your code. A C++
long is *not* the same size as a C# long. A C# long maps to
System.Int64 while a C++ long maps to System.Int32, the same as
int. Use __int64 or (preferably) System::Int64 instead.
In addition, where did the number "65530" come from? In the C#
code you use System.Int32.MaxValue, which is 2147483647
(2^31 - 1). I assume 65530 was meant to be 2^16 - 1, but should
then in fact be 65535 :-). This difference between the C# and C++
code doesn't seem to affect the running time, but it looks silly.

The code below is C++ that is comparable to the C# code. One
would then expect the running times to be the same. Curiously,
on my machine the C# version actually runs 10-15% faster.
Of course, in practice Image would be made a value type which
improves the performance nearly 4 times.

    __gc class Image
    {
    public:
        System::Int64 m_lStatus;
        Image()
        {
            m_lStatus = 0;
        }
    };

    __gc class SomeObj
    {
    public:
        Image* img __gc[];
        SomeObj()
        {
            img = NULL;
        }
    };

    int _tmain()
    {
        Random* rnd =  new Random();
        DateTime dt = DateTime::Now;
        SomeObj* arr __gc[] = new SomeObj*[100];
        for (int i = 0; i < 100; i++)
        {
            arr[i] = new SomeObj;
            arr[i]->img = new Image*[6000];
            for (int j = 0; j < 6000; j++)
            {
                arr[i]->img[j] = new Image();
                arr[i]->img[j]->m_lStatus =
rnd->Next(System::Int32::MaxValue);
            }
        }
        TimeSpan t = DateTime::Now - dt;
        Console::WriteLine(t.TotalMilliseconds.ToString());
        return 0;
    }

Note how ugly this code is compared to the equivalent C#.
I have nothing against C++; I used it for several years and still do.
But for managed code C# has all the advantages (for one thing,
a much cleaner syntax).

- Magnus


Quote:
> Hi All,

> The concept behind the dot net framework is really great. It is nice to
> finally be able to mix languages. However there is a penalty to pay for
> this. The rift between C++ and other languages as far as speed is
concerned
> still continues. If you execute the code below you will notice that the
C++
> code is 4 times faster than the similar one in C#. If the underlying
> framework is the same then why this difference?

> What gives folks?

>  My decision to uses C++ hinges on this performance issue. I would love to
> use C# or VB.Net as it is far simpler to code but if it is going to hamper
> performance.....

> Thanks
> Prady

> C# Code

> class Image
> {
>  public long m_lStatus;
>  public Image()
>  {
>   m_lStatus = 0;
>  }
> }

> class SomeObj
> {
>  public Image [] img;
>  public SomeObj()
>  {
>   img = null;
>  }
> }

> class Class1
> {
>  [STAThread]
>  static void Main(string[] args)
>  {
>   DateTime dt = DateTime.Now;
>   Random rnd = new Random();
>   SomeObj [] arr = new SomeObj[100];

>   for(int i = 0; i < 100; i++)
>   {
>    arr[i] = new SomeObj();
>    arr[i].img = (Image [])new Image[6000];
>    for(int j = 0; j < 6000; j++)
>    {
>     arr[i].img[j] = new Image();
>     arr[i].img[j].m_lStatus = rnd.Next(int.MaxValue);
>    }
>   }
>   TimeSpan diff = DateTime.Now - dt;
>   Console.WriteLine(diff.TotalMilliseconds);
>  }
> }

> C++ Code:
> class Image
> {
> public:
>  long m_lStatus;
>  Image()
>  {
>   m_lStatus = 0;
>  }
> };
> class SomeObj
> {
> public:
>  Image *img;
>  SomeObj()
>  {
>   img = NULL;
>  }
> };

> int _tmain()
> {
>  SomeObj *arr;
>  Random *rnd =  new Random();

>  DateTime dt = DateTime::Now;
>  arr = new SomeObj[100];
>  for(int i = 0; i < 100; i++)
>  {
>   arr[i].img = new Image[6000];
>   for(int j = 0; j < 6000; j++)
>   {
>    arr[i].img[j].m_lStatus = rnd->Next(65530);
>   }
>  }
>  TimeSpan t = DateTime::Now - dt;
>  Console::WriteLine(t.TotalMilliseconds.ToString());
>  return 0;
> }



Tue, 18 Oct 2005 20:02:47 GMT  
 Performance comparison between C# and C++
Thanks Magnus,

That was an excellent eye opener. The code I had written was just a quick
test to check how the two languages compared in respective to speed. I have
a large product to maintain and it is entirely in vc++. Just took a quick
test to figure out if we should indeed port to C#.

I do agree with you, C# seems to be a much cleaner code than C++. However
degradation in performance is a very high price to pay for switching to
managed code. Is it really worth it since the program I am talking about is
an engine that reads some data crunches it and spits it out. Food for
thought....

BTW the 65530 is just some large number no real logic to it. Just wanted to
generate and assign a random number.

Thanks again,
Prady


Quote:
> As has already been pointed out, your comparison is severly flawed.
> In the C++ code the Image and SomeObj classes are not garbage
> collected, which means they will behave like normal C++ classes.
> The C++ code therefore is incorrect. It leaks memory, since it
> allocates objects on the C++ heap and never frees them!
> Compiling C++ code to IL instead of native instructions does not make
> it fully "managed". In order to make the comparison fair, the Image
> and SomeObj classes must be marked with the __gc modifier and
> the code changed accordingly (see below).

> There are also a couple of other inconsistencies in your code. A C++
> long is *not* the same size as a C# long. A C# long maps to
> System.Int64 while a C++ long maps to System.Int32, the same as
> int. Use __int64 or (preferably) System::Int64 instead.
> In addition, where did the number "65530" come from? In the C#
> code you use System.Int32.MaxValue, which is 2147483647
> (2^31 - 1). I assume 65530 was meant to be 2^16 - 1, but should
> then in fact be 65535 :-). This difference between the C# and C++
> code doesn't seem to affect the running time, but it looks silly.

> The code below is C++ that is comparable to the C# code. One
> would then expect the running times to be the same. Curiously,
> on my machine the C# version actually runs 10-15% faster.
> Of course, in practice Image would be made a value type which
> improves the performance nearly 4 times.

>     __gc class Image
>     {
>     public:
>         System::Int64 m_lStatus;
>         Image()
>         {
>             m_lStatus = 0;
>         }
>     };

>     __gc class SomeObj
>     {
>     public:
>         Image* img __gc[];
>         SomeObj()
>         {
>             img = NULL;
>         }
>     };

>     int _tmain()
>     {
>         Random* rnd =  new Random();
>         DateTime dt = DateTime::Now;
>         SomeObj* arr __gc[] = new SomeObj*[100];
>         for (int i = 0; i < 100; i++)
>         {
>             arr[i] = new SomeObj;
>             arr[i]->img = new Image*[6000];
>             for (int j = 0; j < 6000; j++)
>             {
>                 arr[i]->img[j] = new Image();
>                 arr[i]->img[j]->m_lStatus =
> rnd->Next(System::Int32::MaxValue);
>             }
>         }
>         TimeSpan t = DateTime::Now - dt;
>         Console::WriteLine(t.TotalMilliseconds.ToString());
>         return 0;
>     }

> Note how ugly this code is compared to the equivalent C#.
> I have nothing against C++; I used it for several years and still do.
> But for managed code C# has all the advantages (for one thing,
> a much cleaner syntax).

> - Magnus



> > Hi All,

> > The concept behind the dot net framework is really great. It is nice to
> > finally be able to mix languages. However there is a penalty to pay for
> > this. The rift between C++ and other languages as far as speed is
> concerned
> > still continues. If you execute the code below you will notice that the
> C++
> > code is 4 times faster than the similar one in C#. If the underlying
> > framework is the same then why this difference?

> > What gives folks?

> >  My decision to uses C++ hinges on this performance issue. I would love
to
> > use C# or VB.Net as it is far simpler to code but if it is going to
hamper
> > performance.....

> > Thanks
> > Prady

> > C# Code

> > class Image
> > {
> >  public long m_lStatus;
> >  public Image()
> >  {
> >   m_lStatus = 0;
> >  }
> > }

> > class SomeObj
> > {
> >  public Image [] img;
> >  public SomeObj()
> >  {
> >   img = null;
> >  }
> > }

> > class Class1
> > {
> >  [STAThread]
> >  static void Main(string[] args)
> >  {
> >   DateTime dt = DateTime.Now;
> >   Random rnd = new Random();
> >   SomeObj [] arr = new SomeObj[100];

> >   for(int i = 0; i < 100; i++)
> >   {
> >    arr[i] = new SomeObj();
> >    arr[i].img = (Image [])new Image[6000];
> >    for(int j = 0; j < 6000; j++)
> >    {
> >     arr[i].img[j] = new Image();
> >     arr[i].img[j].m_lStatus = rnd.Next(int.MaxValue);
> >    }
> >   }
> >   TimeSpan diff = DateTime.Now - dt;
> >   Console.WriteLine(diff.TotalMilliseconds);
> >  }
> > }

> > C++ Code:
> > class Image
> > {
> > public:
> >  long m_lStatus;
> >  Image()
> >  {
> >   m_lStatus = 0;
> >  }
> > };
> > class SomeObj
> > {
> > public:
> >  Image *img;
> >  SomeObj()
> >  {
> >   img = NULL;
> >  }
> > };

> > int _tmain()
> > {
> >  SomeObj *arr;
> >  Random *rnd =  new Random();

> >  DateTime dt = DateTime::Now;
> >  arr = new SomeObj[100];
> >  for(int i = 0; i < 100; i++)
> >  {
> >   arr[i].img = new Image[6000];
> >   for(int j = 0; j < 6000; j++)
> >   {
> >    arr[i].img[j].m_lStatus = rnd->Next(65530);
> >   }
> >  }
> >  TimeSpan t = DateTime::Now - dt;
> >  Console::WriteLine(t.TotalMilliseconds.ToString());
> >  return 0;
> > }



Wed, 19 Oct 2005 09:23:30 GMT  
 Performance comparison between C# and C++
...and just a final comment. I'm no benchmarking expert, but it is essential
to also perform a warmup test (within the same process instance) prior to
recording performance benchmarks. For .NET framework testing, this allows
the managed environment to peform the time consuming [once only] tasks of
loading required assemblies and performing Just-In-Time compilation, first.
In my own humble opinion, you should give careful consideration to the
comments made in this thread and reconstruct your tests before drawing a
conclusion.

Bryce Marshall


Quote:
> Hi All,

> The concept behind the dot net framework is really great. It is nice to
> finally be able to mix languages. However there is a penalty to pay for
> this. The rift between C++ and other languages as far as speed is
concerned
> still continues. If you execute the code below you will notice that the
C++
> code is 4 times faster than the similar one in C#. If the underlying
> framework is the same then why this difference?

> What gives folks?

>  My decision to uses C++ hinges on this performance issue. I would love to
> use C# or VB.Net as it is far simpler to code but if it is going to hamper
> performance.....

> Thanks
> Prady

> C# Code

> class Image
> {
>  public long m_lStatus;
>  public Image()
>  {
>   m_lStatus = 0;
>  }
> }

> class SomeObj
> {
>  public Image [] img;
>  public SomeObj()
>  {
>   img = null;
>  }
> }

> class Class1
> {
>  [STAThread]
>  static void Main(string[] args)
>  {
>   DateTime dt = DateTime.Now;
>   Random rnd = new Random();
>   SomeObj [] arr = new SomeObj[100];

>   for(int i = 0; i < 100; i++)
>   {
>    arr[i] = new SomeObj();
>    arr[i].img = (Image [])new Image[6000];
>    for(int j = 0; j < 6000; j++)
>    {
>     arr[i].img[j] = new Image();
>     arr[i].img[j].m_lStatus = rnd.Next(int.MaxValue);
>    }
>   }
>   TimeSpan diff = DateTime.Now - dt;
>   Console.WriteLine(diff.TotalMilliseconds);
>  }
> }

> C++ Code:
> class Image
> {
> public:
>  long m_lStatus;
>  Image()
>  {
>   m_lStatus = 0;
>  }
> };
> class SomeObj
> {
> public:
>  Image *img;
>  SomeObj()
>  {
>   img = NULL;
>  }
> };

> int _tmain()
> {
>  SomeObj *arr;
>  Random *rnd =  new Random();

>  DateTime dt = DateTime::Now;
>  arr = new SomeObj[100];
>  for(int i = 0; i < 100; i++)
>  {
>   arr[i].img = new Image[6000];
>   for(int j = 0; j < 6000; j++)
>   {
>    arr[i].img[j].m_lStatus = rnd->Next(65530);
>   }
>  }
>  TimeSpan t = DateTime::Now - dt;
>  Console::WriteLine(t.TotalMilliseconds.ToString());
>  return 0;
> }



Wed, 19 Oct 2005 22:47:38 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. c versus c++ performance(an objective comparison) ?

2. IO performance C# Versus C++ (For C# GURU's only)

3. C# vs C++ .NET performance question

4. C# vs. C++ in performance

5. Three languages: A performance comparison

6. performance comparison ...

7. Performance comparison - Console vs Dialog based

8. Ada/C/C++ comparison (repeated) (was Re: C/C++ knocks the crap out of Ada)

9. Visual C++ .NET reviews, comparisons to Visual C++ 6.0

10. Newbie: separate big .cs file into small .cs files

11. C++ vs C# or ( C++ + C#)?

12. Binary file comparison with C#

 

 
Powered by phpBB® Forum Software