mercredi 5 août 2015

Why is C# running faster than C++?


I am not joking. I have a C# application, and a C++ application. They do they exact same thing, in the exact same amount of code...

...And the C# one is running faster, not just faster, but like 10 times faster.

This struck me as weird, because for one, I was running the C# app in the debugger, which should slow C# down to begin with. Then, for the reason of that C# is bytecode with huge overhead using .NET compiled into MSIL with a bunch of extra features, that should slow it down. While C++ is just pure machine code.

Here is the C# code:

static void main()
{
    ulong i = 0;
    while (i < 100000000000)
    {
        Console.WriteLine(i);
        i++;
    }
}

While this was the C++ code

int main()
{
    usigned long i = 0;
    while (i < 100000000000)
    {
        cout << i << endl;
        i++;
    }
    return 0;
}

They are just counting and displaying a number. The C++ one would be at 1000, while the C# one would be at 7000. ( 7x faster)

I even tried compiling both of them, and running them without the debugger using command prompt with the command: cplusplus.exe && csharp.exe

Yeah, I know maybe this question is "offtopic" :P or maybe it is "not clear what's being asked for". :/ But please, someone explain this to me.

If this matters, I am using this CPU: Intel i7 2.5 Ghz.

EDIT: I did the cout << i << "\n"; idea, plus the std::ios_base::sync_with_stdio(false); idea, without any luck or change in results.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire