我正在使用iTextSharp,并需要生成数十万个RTF文档 - 生成的文件在5KB到500KB之间。有没有办法让这个更快? MemoryStream vs FileStream
我在下面列出2种方法 - 原始方法不一定慢,但我想明白为什么要写/从/从文件获取我需要的输出字符串。我看到了另一种使用MemoryStream的方法,但它实际上减慢了速度。我基本上只需要输出的RTF内容,以便我可以在该RTF上运行一些过滤器来清理不必要的格式。带回数据的查询非常迅速。要使用原始方法文件生成1000个文件(实际上是创建2000个文件)需要大约15分钟,与第二种方法相同需要大约25-30分钟。我运行的结果文件平均大约80KB。
第二种方法有什么问题吗?似乎它应该比第一个更快,而不是更慢。
原始的方法:
RtfWriter2.GetInstance(doc, new FileStream(RTFFilePathName, FileMode.Create));
doc.Open();
//Add Tables and stuff here
doc.Close(); //It saves a file here to (RTFPathFileName)
StreamReader srRTF = new StreamReader(RTFFilePathName);
string rtfText = srRTF.ReadToEnd();
srRTF.Close();
//Do additional things with rtfText before writing to my final file
的新方法,努力加快速度,但其实这是一半快:
MemoryStream stream = new MemoryStream();
RtfWriter2.GetInstance(doc, stream);
doc.Open();
//Add Tables and stuff here
doc.Close();
string rtfText =
ASCIIEncoding.ASCII.GetString(stream.GetBuffer());
stream.Close();
//Do additional things with rtfText before writing to my final file
我想我发现这里的第二种方法: iTextSharp - How to generate a RTF document in the ClipBoard instead of a file
你正在处理的文件有多大?如果它不是很大,那么不会有太大的区别。如果它很大,那么你可能不想在内存中处理它,如果它太多降级你的系统。 – phillip 2010-12-11 22:52:07
你好,感谢你的回复。我需要输出约400,000个文件 - 介于5KB和500KB之间。我正在使用iTextSharp从SQL查询生成RTF内容。 – user53885 2010-12-11 22:53:59
重新使用MemoryStream。即分配一次,并将其用于清除其中的内容的所有文件。 – CodesInChaos 2010-12-12 10:08:01