|
-
April 1st, 2010, 03:51 PM
#1
Performance of Managed vs Unmanaged code
OK, so I haven't had very much experience and and still learning both C++ and C#. I decided to do a test of unmanaged C++ vs C#. I made what I believe to be close to identical code in both languages (but what do I know? lol.) I was hoping some of you may first of all, tell me if you see anything wrong with my code (bad practice, just plain bad code, whatever) and I hope you can also explain the differences I'm seeing.
C++ Program:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
using namespace std;
class Point
{
public:
int x, y;
Point();
Point(int x, int y);
friend Point operator+ (Point obj1, Point obj2);
string ToString();
};
int main()
{
long start, stop;
Point *pointArray = new Point[16000000];
start = GetTickCount();
for (int i = 0; i < 16000000; i++)
{
pointArray[i] = Point(i, i);
}
Point myPoint(1, 1);
for (int i = 0; i < 16000000; i++)
{
pointArray[i] = pointArray[i] + myPoint;
}
stop = GetTickCount();
cout << pointArray[0].ToString();
cout << stop - start << "ms";
cin.get();
return 0;
}
Point::Point()
{
this->x = 0;
this->y = 0;
}
Point::Point(int x, int y)
{
this->x = x;
this->y = y;
}
Point operator+(Point obj1, Point obj2)
{
return Point(obj1.x + obj2.x, obj1.y + obj2.y);
}
string Point::ToString()
{
ostringstream temp;
temp << "Point: x = " << x << ", y = " << y << endl;
return temp.str();
}
and here is my C# version:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(String[] args)
{
Stopwatch time = new Stopwatch();
Point[] pointArray = new Point[16000000];
time.Start();
for (Int32 i = 0; i < 16000000; i++)
{
pointArray[i] = new Point(i, i);
}
Point myPoint = new Point(1, 1);
for (Int32 i = 0; i < 16000000; i++)
{
pointArray[i] = pointArray[i] + myPoint;
}
time.Stop();
Console.WriteLine(pointArray[0].ToString());
Console.WriteLine("{0}ms", time.ElapsedMilliseconds);
Console.ReadLine();
}
}
class Point
{
public Int32 x, y;
public Point()
{
x = 0;
y = 0;
}
public Point(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
}
public static Point operator +(Point obj1, Point obj2)
{
return new Point(obj1.x + obj2.x, obj1.y + obj2.y);
}
public override String ToString()
{
return String.Format("Point: x = {0}, y = {1}", this.x, this.y);
}
}
}
OK, so now that the code is out of the way, here are my observations:
C# Release version takes approximately 2560ms to execute.
C++ Debug version takes approximately 1060ms to execute.
C++ Release version takes approximately 32ms to execute.
Now, I assumed that C# would be slower. Is there anything I could do to make the C# code faster? Also, why is the C++ Release version so much ridiculously faster than both the C# and Debug version? I'm sure there could be some simple things I'm overlooking.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|