Thursday, March 15, 2007

C#, C++ Simple Benchmark

Just for fun I decided to pit a C# application against its exact opposite C++ counter part.

Heres the code for the C++ application:


int main()
float x = 999999;
float y[999999];
float d;

for (float i = 0; i <><<"\n"; } return 0; }

And here is the code for the C# application:

using System;

namespace dude
class run
static void Main(String[] args)
float x = 999999;
float[] y = new float[999999];
float d;

for (float i = 0; i <>

Please note I haven't implemented proper indentation here because I don't have the time to figure out how to do it and do it, also the #include is missing the header file it is trying to include which is the iostream.h header file.

As you can see they are virtually alike and take a minimalistic approach to how they work. My theory is the one that reaches the end first has the better iterator and for loop algorithm.

Of course this is just for my amusement, I don't claim this 'speed run' means or implies anything.

FYI both were compiled in Visual Studio Express, C++ in Visual C++ Express and C# in Visual C# Express.

After building both files I put them on the desktop and checked their file sizes. Not surprisingly the C# version was 16 kilobytes however the C++ version was far larger coming in at 132 kilobytes, it would have to be given I included the iostream header file which is pretty big.

I highlighted both and pressed the enter key, the first one to finish would be the first to terminate the window, my result was kind of unexpected. The C# compiled code was significantly faster than its C++ counterpart,

Using a stopwatch I found the C# version completed its loop after 01:42:86 (MM:SS:MS) while the C++ was still going, finishing after 05:14:60. Yeah it is an extremely crude way of doing it, I'm not exactly trying to prove anything here, it is purely for my own amusement.

So what can I conclude from this? Well one thing is for certain, unless you're willing to dig in deep into the mysterious world of STD header files you'll be coming up with code quite a bit larger in C++ than in C# where all extra code is removed during the compilation process.

Another thing is, perhaps because of C#'s more user friendly compilation where it does all the dirty work C++ programs, at least simple ones like the one provided here will run less efficiently in C++ than C#.

Then again, I don't claim to be an expert on this matter, however I am reporting valid and true results from my little experience, take what you want from it.

No comments: