Home Is there a way to speed up TStreamWriter::WriteLine ()?
Reply: 0

Is there a way to speed up TStreamWriter::WriteLine ()?

user1369
1#
user1369 Published in July 21, 2018, 6:11 am

I am using C++ Builder Tokyo 10.2.2. I have a project that involves writing files that are too large for TStringList (it causes memory errors).

Therefore, I created an alternate save system that used TStreamWriter. This works well, except for one small problem. It seems that TStreamWriter's WriteLine () is very slow compared to TStringList's Add ().

Here is code that compares the two.

TStopwatch sw = TStopwatch::StartNew ();

TStreamWriter* writer = new TStreamWriter (sFileName, false, TEncoding::UTF8, 1024);
for (int i=0; i<1000000; i++)
    writer->WriteLine (i);
delete writer;

sw.Stop ();
TTimeSpan ts = sw.Elapsed;
String s = String (ts.TotalMilliseconds) + " Milliseconds";

TStopwatch sw2 = TStopwatch::StartNew ();

TStringList* list = new TStringList ();
for (int i=0; i<1000000; i++)
    list->Add (i);
list->SaveToFile (sFileName);
delete list;

sw2.Stop ();
TTimeSpan ts2 = sw2.Elapsed;
String s2 = String (ts2.TotalMilliseconds) + " Milliseconds";

The most recent value for ts (using TStreamWriter) on my test system is 2748. The most recent value for ts2 (using TStringList) is 522. So, TStreamWriter is taking more than 5 times longer to achieve the same functionality.

Am I missing something with TStreamWriter that could significantly increase its speed?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322864 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO