我正在製作一種從壓縮文件中提取信息的方法。所有的zip文件將只包含一個文本文件。這意味着方法應該返回一個字符串數組。使用c#慢速解壓縮文本文件dotnetzip .NET 4.0
我正在使用dotnetzip,但我正在經歷一個可觀的表現。我試圖對每一步的表現進行基準測試,並且似乎在所有步驟上都表現得很慢。
C#代碼爲:
public string[] LoadZipFile(string FileName)
{
string[] lines = { };
int start = System.Environment.TickCount;
this.richTextBoxLOG.AppendText("Reading " + FileName + "... ");
try
{
int nstart;
nstart = System.Environment.TickCount;
ZipFile zip = ZipFile.Read(FileName);
this.richTextBoxLOG.AppendText(String.Format("ZipFile ({0}ms)\n", System.Environment.TickCount - nstart));
nstart = System.Environment.TickCount;
MemoryStream ms = new MemoryStream();
this.richTextBoxLOG.AppendText(String.Format("Memorystream ({0}ms)\n", System.Environment.TickCount - nstart));
nstart = System.Environment.TickCount;
zip[0].Extract(ms);
this.richTextBoxLOG.AppendText(String.Format("Extract ({0}ms)\n", System.Environment.TickCount - nstart));
nstart = System.Environment.TickCount;
string filecontents = string.Empty;
using (var reader = new StreamReader(ms))
{
reader.BaseStream.Seek(0, SeekOrigin.Begin);
filecontents = reader.ReadToEnd().ToString();
}
this.richTextBoxLOG.AppendText(String.Format("Read ({0}ms)\n", System.Environment.TickCount - nstart));
nstart = System.Environment.TickCount;
lines = filecontents.Replace("\r\n", "\n").Split("\n".ToCharArray());
this.richTextBoxLOG.AppendText(String.Format("SplitLines ({0}ms)\n", System.Environment.TickCount - nstart));
}
catch (IOException ex)
{
this.richTextBoxLOG.AppendText(ex.Message+ "\n");
}
int slut = System.Environment.TickCount;
this.richTextBoxLOG.AppendText(String.Format("Done ({0}ms)\n", slut - start));
return (lines);
例如,我得到這個輸出:
閱讀xxxx.zip ...的ZipFile(0毫秒) 的MemoryStream(0毫秒) 提取物(234ms) 閱讀(78ms) SplitLines(187ms) 完成(514ms)
總共514毫秒。當使用此代碼在python 2.6中執行相同的操作時:
def ReadZip(File):
z = zipfile.ZipFile(File, "r")
name =z.namelist()[0]
return(z.read(name).split('\r\n'))
它僅在89 ms內執行。任何關於如何提高性能的想法都非常受歡迎。
我猜你應該使用ICSharpCode.SharpZipLib圖書館 – 2012-08-03 11:22:20
不像Python代碼的.NET代碼更新了UI和功能異常處理和緩存在內存流,所以這不是一個公平的比較。順便說一句:在記錄SplitLines之後nstart沒有更新,所以Done = SplitLines + Done – mtijn 2012-08-03 11:27:47