StreamWriter.WriteLine() write string slowly on linux(redhat)

Xie Steven 831 Reputation points
2022-01-04T01:23:03.46+00:00

Hello,

I'm developing work service(.net 5) application. It will use StreamWriter.WriteLine() method to write string to a file.

According to my tests, I found that there was a half gap in running time when the work service run on windows and linux(redhat).

On windows: 20000 ms+

On linux: 40000 ms+

I want it to run on Linux as well as on windows.

The following is the code sample:

private static void WriteLineTest()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        StreamWriter writer = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testLine.csv"), false, Encoding.Default,bufferSize: 65536);

        for (int i = 0; i < 3000000; i++)
        {
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < 30; j++)
            {
                builder.Append("Col: ").Append(j);
            }
            writer.WriteLine(builder);
        }

        writer.Close();
        stopwatch.Stop();
        Console.WriteLine("WriteLineTest: " + stopwatch.ElapsedMilliseconds + " ms.");
    }

Or, If you know any other hign performance I/O library, please feel free to share it with me.

Thanks,
Steven

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2022-01-04T21:06:49.9+00:00

    as your sample code strictly i/o bound, I'd check the performance of the disk drives between the two systems. in general linux i/o is faster. You might also want to retest in .net 6, where the i/o has all been rewritten.

    I ran the code on .net 6 on 2013 imac, and it took:

    4669 ms.

    on my m1

    1982 ms

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.