如何加快C++ Builder中TStreamWriter::WriteLine()的执行速度?
TStreamWriter.WriteLine() Performance in C++ Builder Tokyo 10.2.2 I’ve been in your exact situation before—working on a C++ Builder Tokyo 10.2.2 project where writing huge files with TStringList caused memory crashes (since it loads everything into RAM first), switching to TStreamWriter fixed the memory issues but brought a new problem: WriteLine() is way slower than TStringList.Add(). Your test showing TStreamWriter taking 2748ms vs. the much faster TStringList aligns with what I’ve seen too.
Here are the most effective ways to speed up TStreamWriter without bringing back the memory problems:
Optimizations for TStreamWriter Speed
1. Increase the Buffer Size
By default, TStreamWriter uses a small buffer, which means it’s hitting the disk far more often than necessary. Bumping up the buffer size (to 64KB, 128KB, or even 512KB depending on your file size) reduces disk I/O operations drastically:
// Create TStreamWriter with a 128KB buffer (adjust size as needed) TStreamWriter* writer = new TStreamWriter(L"large_output.txt", false, TEncoding::UTF8, 131072);
2. Disable Auto-Flush
TStreamWriter defaults to auto-flushing after certain operations (like WriteLine()), which forces disk writes more frequently. Turn off auto-flush, do all your writing, then manually flush once at the end:
writer->AutoFlush = false; // Batch write your lines here for (int i = 0; i < 100000; ++i) { writer->WriteLine(L"Record " + IntToStr(i)); } // Flush all buffered data to disk once done writer->Flush();
3. Batch Write with a StringBuilder
Instead of calling WriteLine() thousands of times, build chunks of text in a TStringBuilder first, then write the entire chunk at once. This cuts down on the number of method calls and disk interactions:
TStringBuilder* sb = new TStringBuilder(); const int batchSize = 1000; for (int i = 0; i < 100000; ++i) { sb->AppendLine(L"Line " + IntToStr(i)); // Write batch when we hit the size limit if ((i + 1) % batchSize == 0) { writer->Write(sb->ToString()); sb->Clear(); } } // Write any remaining lines if (sb->Length() > 0) { writer->Write(sb->ToString()); } delete sb;
4. Fall Back to C++ Standard Library (For Extreme Cases)
If even optimized TStreamWriter isn’t fast enough, use the C++ standard wofstream—it’s often more efficient for large file writes, especially when handling UTF-8:
#include <fstream> #include <string> #include <locale> #include <codecvt> std::wofstream outFile(L"large_output.txt"); // Set up UTF-8 encoding outFile.imbue(std::locale(outFile.getloc(), new std::codecvt_utf8<wchar_t>)); for (int i = 0; i < 100000; ++i) { outFile << L"Line " << i << std::endl; } outFile.close();
Why TStringList.Add() Is Faster
Remember: TStringList.Add() is purely an in-memory operation—it just appends strings to a list in RAM, which is orders of magnitude faster than disk I/O. The tradeoff is the memory crash when dealing with huge files. TStreamWriter is writing to disk, so we can’t match TStringList’s speed, but the optimizations above will get you as close as possible while keeping memory usage under control.
内容的提问来源于stack exchange,提问作者Anthony Burg

