Why my C# code is faster than my C code?

ozgur

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.

Philip Stuyck

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)?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why in this case, my Java code runs faster than my C++ code?

From Dev

Why in this case, my Java code runs faster than my C++ code?

From Dev

Why is my C++ code so much slower than R?

From Dev

Why is this code so faster in java than in C++ and C#

From Dev

Why is my computation so much faster in C# than Python

From Dev

Is SQL code faster than C# code?

From Dev

String#indexOf: Why comparing 15 million strings with JDK code is faster than my code?

From Dev

String#indexOf: Why comparing 15 million strings with JDK code is faster than my code?

From Dev

c++ vs MATLAB optimization for speed. E.g. my matlab code runs faster than c++?

From Dev

Why is my C++ code three times slower than the C equivalent on LeetCode?

From Dev

Why a breakpoint jumps in my C++ code?

From Dev

Why a breakpoint jumps in my C++ code?

From Dev

Why an additional async operation is making my code faster than when the operation is not taking place at all?

From Dev

Why is my inclusive scan code 2x faster on CPU than on a GPU?

From Dev

Why is my D code to sum a vector up x4 slower than C?

From Dev

AJAX Seems to be moving faster than the rest of my code

From Dev

Why is my foreach faster than my for loop?

From Dev

Why is my code printing more than once?

From Dev

Why is my more complicated C loop faster?

From Dev

Cant execute more than 1 line in my C++ code

From Dev

Why is Code 1 faster than Code 2?

From Java

Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

From Dev

Plain C++ Code 10 times faster than inline assembler. Why?

From Dev

my C code is too awkward

From Dev

assembler code of my C function

From Dev

A strange result in my C code

From Dev

what is wrong with my code in c

From Dev

LicenseException in my C# code

From Dev

Syntax error in my C code?

Related Related

  1. 1

    Why in this case, my Java code runs faster than my C++ code?

  2. 2

    Why in this case, my Java code runs faster than my C++ code?

  3. 3

    Why is my C++ code so much slower than R?

  4. 4

    Why is this code so faster in java than in C++ and C#

  5. 5

    Why is my computation so much faster in C# than Python

  6. 6

    Is SQL code faster than C# code?

  7. 7

    String#indexOf: Why comparing 15 million strings with JDK code is faster than my code?

  8. 8

    String#indexOf: Why comparing 15 million strings with JDK code is faster than my code?

  9. 9

    c++ vs MATLAB optimization for speed. E.g. my matlab code runs faster than c++?

  10. 10

    Why is my C++ code three times slower than the C equivalent on LeetCode?

  11. 11

    Why a breakpoint jumps in my C++ code?

  12. 12

    Why a breakpoint jumps in my C++ code?

  13. 13

    Why an additional async operation is making my code faster than when the operation is not taking place at all?

  14. 14

    Why is my inclusive scan code 2x faster on CPU than on a GPU?

  15. 15

    Why is my D code to sum a vector up x4 slower than C?

  16. 16

    AJAX Seems to be moving faster than the rest of my code

  17. 17

    Why is my foreach faster than my for loop?

  18. 18

    Why is my code printing more than once?

  19. 19

    Why is my more complicated C loop faster?

  20. 20

    Cant execute more than 1 line in my C++ code

  21. 21

    Why is Code 1 faster than Code 2?

  22. 22

    Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

  23. 23

    Plain C++ Code 10 times faster than inline assembler. Why?

  24. 24

    my C code is too awkward

  25. 25

    assembler code of my C function

  26. 26

    A strange result in my C code

  27. 27

    what is wrong with my code in c

  28. 28

    LicenseException in my C# code

  29. 29

    Syntax error in my C code?

HotTag

Archive