plkana
plkana

Reputation: 51

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.

EDIT 2: I tried C's printf() and it was much, much faster. 3x faster than C#.

People told me that IO stream was very slow, so I then tried them both without writing to the console, and C++ was still significantly faster than C#.

In conclusion, Writeline() is much faster than cout, and printf() is much faster than both. So writing to the console is the only thing that slows it down.

TLDR: printf() wins, and console writing slows stuff down.

Upvotes: 1

Views: 5852

Answers (4)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

Your code is inefficient because:

  • The C++ stream object is in sync with C's stdio by default which makes it slow.
  • You're using endl which is further making it slow.

Fixing both these issues, you've this code:

int main()
{
    std::ios_base::sync_with_stdio(false);

    usigned long i = 0;
    while (i < 100000000000)
    {
        cout << i << '\n'; //use \n, not endl
        i++;
    }
    return 0;
}

Compile this as (must use optimization flag whatever compiler you're using):

$ g++ main.cpp -O3 -o run.test
$ time ./run.test

For explanation of both sync_with_stdio(false) and endl, read my answer here:

Hope that helps.

Upvotes: 9

Tony Delroy
Tony Delroy

Reputation: 106244

I think you've not been careful enough in your evaluation. I recreated your test with C++ proving massively faster as detailed below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSScratch
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong i = 0;
            while (i < 1000000)
            {
                Console.WriteLine(i);
                i++;
            }
        }
    }
}

I built the above in VS2013 Release mode to CSScratch.exe, which I then timed (under cygwin bash) with output redirected so file-system writing time wasn't counted. Results were fairly consistent, the fastest of five runs being:

time ./CSScratch.exe > NUL

real    0m17.175s
user    0m0.031s
sys     0m0.124s

The C++ equivalent:

#include <iostream>

int main()
{
    std::ios_base::sync_with_stdio(false);


    unsigned long i = 0;
    while (i < 1000000)
    {
        std::cout << i << '\n';
        i++;
    }
}

Also compiled with VS2013:

cl /EHsc /O2 output.cc
time ./output > NUL

The slowest of five runs:

real    0m1.116s
user    0m0.000s
sys     0m0.109s

which is still faster (1.116 seconds) than the fastest of C# runs (17.175 seconds).

Some of the time for both versions is taken by loading / dynamic linking, initialisation etc.. I modified the C++ version to loop 10x more, and it still only took 9.327 seconds - about half the time C# needed for a tenth of the workload.

(You could further tune the C++ version by setting a larger output buffer, but that's not normally needed).

Upvotes: 3

Eugene
Eugene

Reputation: 3447

If you replace

cout << i << endl;

with

printf("%d\n", i);

the result will be close to .net WriteLine.

In general, just a fact you are writing on C++ or C does not automatically mean that your code will be faster. Fast code is not only the language syntax. It also requires some of knowledge of underlying things like hardware and OS internals.

I mean properly used C++ gives much better results, at least on not trivial tasks.

Upvotes: 1

user447688
user447688

Reputation:

Tony D makes an excellent point about buffering in the comments.
Add this to your C# code and time it again:

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

Upvotes: 2

Related Questions