Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:28:39+00:00 2026-06-05T14:28:39+00:00

this is in references to my previous question Log to memory and then write

  • 0

this is in references to my previous question Log to memory and then write to file, actually the edit part of that question, I asked in edit part that if I write to memory would that be faster than writing to file? I performed a simple test, and I had shocking results! I wanted to share with the community.
So here’s the code

private void Button1Click(object sender, EventArgs e)
{
    var stopwatch = new Stopwatch();
    stopwatch.Start();
    File.AppendAllText(@"D:\File1.txt", string.Format("{0}Start! : {1}", Environment.NewLine, DateTime.Now.ToString(CultureInfo.InvariantCulture)));
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 1000000; j++)
        {
             File.AppendAllText(@"D:\File1.txt", string.Format("{0}{1}:{2}", Environment.NewLine, i.ToString(CultureInfo.InvariantCulture), j.ToString(CultureInfo.InvariantCulture)));
         }
    }
    File.AppendAllText(@"D:\File1.txt", string.Format("{0}Done!{1}", Environment.NewLine, DateTime.Now.ToString(CultureInfo.InvariantCulture)));
    stopwatch.Stop();
    File.AppendAllText(@"D:\File1.txt",
                       string.Format("{0}{1}:{2}",Environment.NewLine,              stopwatch.Elapsed.ToString(), stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture)));
    MessageBox.Show("Done!");
}

private void Button2Click(object sender, EventArgs e)
{
     var stopwatch = new Stopwatch();
     using (var mem = new MemoryStream())
     {
         using (var binaryWriter = new BinaryWriter(mem))
         {
             stopwatch.Start();
             {
                 binaryWriter.Write("start! : " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
                 for (int i = 0; i < 6; i++)
                 {
                     for (int j = 0; j < 1000000; j++)
                     {
                         binaryWriter.Write(i.ToString(CultureInfo.InvariantCulture) + ":" + j.ToString(CultureInfo.InvariantCulture));
                     }
                 }
                 stopwatch.Stop();
                 binaryWriter.Write("Done! " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
                 binaryWriter.Write(stopwatch.Elapsed.ToString() + ":" + stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture));
                 binaryWriter.Flush();
                 var file = new FileStream(@"D:\File2.txt", FileMode.Create);
                 mem.WriteTo(file);
             }
         }
     }
     MessageBox.Show("Done!");
}

As the code should be easy to understand

Performance Comparison

Elapsed time in File1.txt = 00:50:24.5654918  
Elapsed milliseconds in File1.txt = 3024565  
Elapsed time in File2.txt = 00:00:04.7430152  
Elapsed milliseconds in File2.txt = 4743

So, as you can see for yourself, there is about 50 minutes of differences! This could be a real cause for bad perfromance, if you log everything directly to IO File, without use of memory stream or any custom tool for logging, OTOH compared to 50 minutes of File.AppendAllText, using MemoryStream only took about 4 and a quarter second. (I am still confused as to why the time shown in windows explorer doesn’t corresponds to time shown in file in the end by stopwatch.ShowElapasedTime, but nonetheless, even if we see windows explorer time, its still about 45 minutes faster!) So, this can be a really useful thing, I thought of sharing it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:28:41+00:00Added an answer on June 5, 2026 at 2:28 pm

    That’s because File.AppendAllText opens the file, writes, flushes the buffer and closes it. If you keep the log file open and use a stream to write to it (instead of the MemoryStream), you will get results that are very close to what you’ve seen with MemoryStream – it might even be indistinguishable.

    Try it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-on question to C++0x rvalue references and temporaries In the previous
In relation to this previous question I am trying to create a batch file
This is similar to my previous question, but that solution did not solve this
This question, like my previous question, references Effective Java . This time I have
In my previous question , I asked how I would design the database schema
This is a continuation question from a previous question I have asked I now
this question is an extension to a previous question i asked (and was answered).
I have a problem finding references to this subject and would appreciate some help.
This must be a dumb question, but I don't see any obvious references. How
From the previous question I asked, I am still having the same question. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.