Why is C # faster than C ++?

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

... And C # one is faster, not only faster, but also 10 times faster.

This seemed strange to me because, firstly, I ran the C # application in a debugger, which should start with C #. Then, due to the fact that C # is a bytecode with huge overhead, using .NET, compiled into MSIL with a bunch of extra features, this should slow down. Although 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 it was C ++ code

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

They just count and show the number. C ++ will be at 1000, and C # at 7000. (7 times faster)

I even tried to compile both of them and run them without a debugger using the command line using the command: cplusplus.exe && & csharp.exe

Yes, I know, maybe this question is β€œofftopic”: P, or maybe β€œit’s not clear what they are asking for.”: / But, please, someone will explain this to me.

If that matters, I use this processor: Intel i7 2.5 Ghz.

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

EDIT 2: I tried C printf () and it was much faster. 3 times faster than C #.

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

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

TL; DR: printf () wins, and console writing slows down.

0
source share
4 answers

I think you were not careful enough in your assessment. I recreated your test with C ++, proving massiveness faster, as described 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 above in VS2013 Release mode for CSScratch.exe, which was then configured (under cygwin bash) with redirected output, so the file system write time was not taken into account. The results were pretty consistent, the fastest of five runs:

 time ./CSScratch.exe > NUL real 0m17.175s user 0m0.031s sys 0m0.124s 

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 the five runs:

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

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

For some time, loading / dynamic linking, initialization, etc. is taken for both versions. I changed the C ++ version to a 10x cycle more , and it still took only 9.327 seconds - about half the C # time needed for a tenth of the workload.

(You could further customize the C ++ version by setting a large output buffer, but this is usually not required).

+2
source

Your code is inefficient because:

  • The C ++ stream object syncs with C stdio by default, which makes it slow.
  • You are using endl , which slows down.

By fixing both of these issues, you have 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 ( should use the optimization flag for any compiler you use ):

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

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

Hope this helps.

+7
source

Tony D is great for buffering comments.
Add this to your C # code and run it again:

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

If you replace

 cout << i << endl; 

with

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

the result will be close to .net WriteLine.

In general, just the fact that you write in C ++ or C does not automatically mean that your code will be faster. Fast code is not just the language syntax. It also requires some knowledge of basic things, such as hardware and the internal environment of the OS.

I mean that correctly used C ++ gives much better results, at least on non-trivial tasks.

+1
source

All Articles