6

I am launching these two console applications on Windows OS. Here is my C# code

int lineCount = 0;
StreamWriter writer = new StreamWriter("txt1.txt",true);
for (int i = 0; i < 900; i++)
{
    for (int k = 0; k < 900; k++)
    {
        writer.WriteLine("This is a new line" + lineCount);
        lineCount++;
    }
}

writer.Close();
Console.WriteLine("Done!");
Console.ReadLine();

And here is my C code. I am assuming it is C because I included cstdio and used standard fopen and fprintf functions.

FILE *file = fopen("text1.txt","a");

for (size_t i = 0; i < 900; i++)
{
    for (size_t k = 0; k < 900; k++)
    {
        fprintf(file, "This is a line\n");
    }
}

fclose(file);
cout << "Done!";

When I start C# program I immediately see the message "Done!". When I start C++ program (which uses standard C functions) it waits at least 2 seconds to complete and show me the message "Done!".

I was just playing around to test their speeds, but now I think I don't know lots of things. Can somebody explain it to me?

NOTE: Not a possible duplicate of "Why is C# running faster than C++? ", because I am not giving any console output such as "cout" or "Console.Writeline()". I am only comparing filestream mechanism which doesn't include any interference of any kind that can interrupt the main task of the program.

ozgur
  • 2,549
  • 4
  • 25
  • 40
  • 3
    The applications do not perform identical operations. While the C# version constructs a temporary string object, that's written to the file, the C++ code calls `fprintf` (without actually performing any formatting). The latter still needs to parse the format string. Another difference: The .NET code does not wait for the native file handle to get closed. The unmanaged code however does. – IInspectable Aug 10 '15 at 14:29
  • 1
    buffering perhaps, why are you not using a fstream ? – Philip Stuyck Aug 10 '15 at 14:30
  • 8
    Maybe because `fprintf` flushes each line into file while `StreamWriter` writes into memory buffer and optimizes disk writes. – Heavy Aug 10 '15 at 14:30
  • Anyway: Do not use console/terminal output while measuring speed –  Aug 10 '15 at 14:31
  • possible duplicate of [Why is C# running faster than C++?](http://stackoverflow.com/questions/31822648/why-is-c-sharp-running-faster-than-c) – Raymond Chen Aug 10 '15 at 14:38
  • Also, your program spends nearly all of its time in the runtime library, so the language is not really significant. – Raymond Chen Aug 10 '15 at 14:39
  • Since when is cout C ? This is C++, so I added C++ again, even though the file access is done in C – Philip Stuyck Aug 10 '15 at 14:49
  • @PhilipStuyck Time spent in the program is not related to **cout**. examine the code. cout is where the program ends. – ozgur Aug 10 '15 at 15:03
  • Yes, but you should not use fopen but use a fstream instead. – Philip Stuyck Aug 10 '15 at 15:10

2 Answers2

12

You are comparing apples and potatoes. Your C/C++ program is not doing any buffering at all. If you were to use a fstream with buffering your results would be a lot better : See also this std::fstream buffering vs manual buffering (why 10x gain with manual buffering)?

Community
  • 1
  • 1
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

I don't think that's an appropriate way to compare performance between languages.

Anyway c and c# are completely different beasts, when the main difference in my opinion is that C# is managed language (there is CLR that runs in the background and does a lot of work like optimization etc.) while C is not.

However as I said, there are too much differences between the two to compare here.

Felix Av
  • 1,254
  • 1
  • 14
  • 22
  • The CLR doesn't do much beyond JIT compiling code, the first time it is executed. It certainly doesn't do anything in the background, and optimization is certainly not one of the things it does. What it does provide is a *compacting heap* that makes memory allocations blazingly fast, usually an order (or even two) faster than any given CRT. – IInspectable Aug 10 '15 at 14:41
  • @IInspectable check out those, I think there is more to CLR: http://www.onlinebuff.com/article_roles-of-common-language-runtime-in-dotnet-framework_6.html http://www.c-sharpcorner.com/Blogs/7955/responsibilities-of-clr-in-net-framework.aspx – Felix Av Aug 10 '15 at 14:44
  • Uhm, yes? Except for the automatic garbage collection, none of this runs in the background, and garbage collection is usually triggered manually as well (`IDisposable` pattern). And where did you get the idea, that the CLR would perform any sort of optimizations? Also, if you do want to make a point, don't post links to articles published by authors whose sole reference is being the author of this particular article. – IInspectable Aug 10 '15 at 14:49
  • Third sentence in the linked article: *"The main function of Common Language Runtime (CLR) is to convert the Managed Code into native code and then execute the Program."* Boy, is this bad. No, that is certainly not, what the CLR does. The CLR consumes IL. A publisher to be trusted would know the difference. This kid doesn't. – IInspectable Aug 10 '15 at 14:52