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

  • Home
  • SEARCH
  • 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 9210541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:06:14+00:00 2026-06-18T01:06:14+00:00

The following console application utilises TStringList.SaveToFile to write multiples lines to a text file:

  • 0

The following console application utilises TStringList.SaveToFile to write multiples lines to a text file:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Classes;
var
  i: Integer;
  a,b,c: Single;
  myString : String;
  myStringList : TStringList;
begin
  try
    Randomize;
    myStringList := TStringList.Create; 
    for i := 0 to 1000000 do
    begin
      a := Random;
      b := Random;
      c := Random;
      myString := FloatToStr(a) + Char(9) + FloatToStr(b) + Char(9) + FloatToStr(c);
      myStringList.Add(myString);
    end;
    myStringList.SaveToFile('Output.txt');
    myStringList.Free;
    WriteLn('Done');
    Sleep(10000);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

It takes around 3 seconds to write a >50MB file with 1000001 lines and seems to work fine. However, many people advocate using streams for such processes. What would the stream equivalent be and what are the advantages/disadvantages of using it compared to TStringList.SaveToFile?

  • 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-18T01:06:16+00:00Added an answer on June 18, 2026 at 1:06 am

    It may be faster to write directly to a stream. Or it may not. I suggest you try it out and time both options. Writing to a stream looks like this:

    for i := 0 to 1000000 do
    begin
      a := Random;
      b := Random;
      c := Random;
      myString := FloatToStr(a) + Char(9) + FloatToStr(b) + Char(9) + 
        FloatToStr(c) + sLineBreak;
      Stream.WriteBuffer(myString[1], Length(myString)*SizeOf(myString[1]));
    end;
    

    To have any hope of this version being fast, you need to use a buffered stream. Try this one: Buffered files (for faster disk access).

    The code above will output UTF-16 text on modern Delphi. If you want to output ANSI text simply declare myString as AnsiString.

    I’ll let you do the timing, but my guess is that this variant performs similarly to the string list. I suspect that the time is spent calling Random and FloatToStr. I expect that the file saving with the string list is already very fast.

    Putting speed to one side, there is another benefit of this approach. In the string list approach, as per the code in the question, the entire content of the text file is stored in memory. And when you save the file, another copy is made as part of the save procedure. So you will have two copies of the entire file in memory.

    In contrast, when saving directly to a stream, the only memory requirement is whatever buffer your stream class uses. For a 50MB file as per the question there’s likely no real problem with either approach. For a much larger file then you will run into out of memory errors if you try to hold the entire file in memory.


    Personally though, I’d consider making use of the TStreamWriter class. This useful class separates the concerns of writing data (text, values etc.) from the concern of pushing to a stream. Your code would become:

    Writer := TStreamWriter.Create(Stream);//use whatever stream you like
    try
      for i := 0 to 1000000 do
      begin
        a := Random;
        b := Random;
        c := Random;
        Writer.WriteLine(FloatToStr(a) + Char(9) + FloatToStr(b) + Char(9) +
          FloatToStr(c));
      end;
    finally
      Writer.Free;
    end;
    

    The TStreamWriter implements buffering with a 1KB buffer so you can use TFileStream and expect to get reasonable performance.


    I would recommend that you choose the technique that leads to the most readable code. If performance becomes an issue you can optimise that later. My personal preference would be for TStreamWriter. This gives very clean and readable code, yet also excellent separation of content generation from streaming. The performance is perfectly reasonable also.

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

Sidebar

Related Questions

I don't understand why the following small console application does not compile: program Project1;
I have the following syntax in the .cmd file, where PathList is console application
I'm creating a console application to read a local file using the following code:
I would like to write the following code in c#. a) small console application
Consider the following console application: class Program { static void Main() { MyInterface test
I've got the following in a console application: class Program { static void Main(string[]
I have following code in Program.cs in console application class Program : IView {
I have a WCF Console Application with the following app.config file <?xml version=1.0 encoding=utf-8
I create Console Application in VC++ 2010, and add the following code to it:
The following code is working great in a normal console application: private void button1_Click(object

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.