views:

228

answers:

3

I was thinking about the speed difference of C++ to C# being mostly about C# compiling to byte-code that is taken in by the JIT compiler (is that correct?) and all the checks C# does.

I notice that it is possible to turn a lot of these functions off, both in the compile options, and possibly through using the unsafe keyword as unsafe code is not verifiable by the common language runtime.

Therefore if you were to write a simple console application in both languages, that flipped an imaginary coin an infinite number of times and displayed the results to the screen every 10,000 or so iterations, how much speed difference would there be? I chose this because it's a very simple program.

I'd like to test this but I don't know C++ or have the tools to compile it. This is my C# version though:

static void Main(string[] args)
{
    unsafe
    {
        Random rnd = new Random();
        int heads = 0, tails = 0;
        while (true)
        {
            if (rnd.NextDouble() > 0.5)
                heads++;
            else
                tails++;

            if ((heads + tails) % 1000000 == 0)
                Console.WriteLine("Heads: {0}  Tails:  {1}", heads, tails);
        }
    }
}

Is the difference enough to warrant deliberately compiling sections of code "unsafe" or into DLLs that do not have some of the compile options like overflow checking enabled? Or does it go the other way, where it would be beneficial to compile sections in C++? I'm sure interop speed comes into play too then.

To avoid subjectivity, I reiterate the specific parts of this question as:

  • Does C# have a performance boost from using unsafe code?
  • Do the compile options such as disabling overflow checking boost performance, and do they affect unsafe code?
  • Would the program above be faster in C++ or negligably different?
  • Is it worth compiling long intensive number-crunching tasks in a language such as C++ or using /unsafe for a bonus? Less subjectively, could I complete an intensive operation faster by doing this?
A: 

Up to more than 100% of speed - depends a lot on the task, simply said.

More than 100% - yes, because the just in time compiler knows your processor, and I doubt you actually optimize for your hardware platform ;)

No SSE is a problem if you do matrix operations.

FOr some things with tons of arrays (image manipulation) The array tests kill you, but pointers work (i.e. unsafe code) as they bypass this.

Regarding things like overflow checking - be carefull. As in: in C++ you have the same possibly. If you need overflow checking, the performance issue is not there ;)

I personally would not bother with C++ in most cases. Partially yes, especially when you can benefit from SSE:

So, at the end a lot depends on the NATURE if your calculations.

TomTom
-1..C++ does not have to deal with garbage collection. C# does more things in the background then c++. You can do more optimization with c++. C# is a higher level language and therefore slower.
Luke101
+2  A: 

The example given is flawed because it does not show real life usage of both programming languages. Using simple datatypes to measure the speed of a language will not bring anything interesting. Instead, I suggest you create a template class in C++ and compare it with what is possible in C# for class generics. In the end, objects will bring some important results and you will see that C++ is faster than C#. Not to mention that you are comparing a lower level programming language with C#.

Does C# have a performance boost from using unsafe code?

Yes, it will have a boost but it is not suggested that you write only code with unsafe. Here is why: Code written using an unsafe context cannot be verified to be safe, so it will be executed only when the code is fully trusted. In other words, unsafe code cannot be executed in an untrusted environment. For example, you cannot run unsafe code directly from the Internet. http://msdn.microsoft.com/en-us/library/aa288474(VS.71).aspx

Would the program above be faster in C++ or negligably different?

Yes the program would be slightly faster in C++. C++ is a lower programming language and even faster if you start using the algorithm library (random_shuffle comes to mind).

Is it worth compiling long intensive number-crunching tasks in a language such as C++ or using /unsafe for a bonus? Less subjectively, could I complete an intensive operation faster by doing this?

It depends on the project...

Alerty
A: 

I would recommend taking a look at this question: Is it possible to compile .NET IL code to machine code?

Chris Marisic