for(long i=0;i<ouput.LongLength;i++)
{
     string temp2 = ouput[i].ToString("x");
     hexstr +=  temp2 +" ";
}这样是很耗时间的hexstr +=  temp2 +" ";
每次都要搜索到字符串的后尾,然后才连接字符串,
而且字符串是越来越长,那么就越来越慢了

解决方案 »

  1.   

    谢谢解答,换成如下写法在性能上差不多节约了一半的时间开销,但是打开25K的文件还是要二十几秒,请问有没有什么办法再优化呀,
    for(long i=ouput.LongLength; i>0; i--)
    {
         string temp2 = ouput[i-1].ToString("x");
         write =  temp2 + " " + write;
    }
      

  2.   

    用StringBuilder.我觉得不一定要一次读完,读取足够用于显示的字节就够了。
      

  3.   

    看看我的代码,我测试的文件也是25K,但用不了一秒啊!
    System.DateTime t1=System.DateTime.Now;
    byte[] buffer;
    try
    {
    System.IO.Stream aFile=System.IO.File.Open(@"D:\RavBack\zip.dll",System.IO.FileMode.Open);
    buffer=new byte[aFile.Length];
    aFile.Read(buffer, 0, buffer.Length);
    aFile.Close();
    }
    catch(System.IO.IOException ex)
    {
    string errStr = "读取数据失败:\n";
    errStr += ex.Message;
    MessageBox.Show(errStr);
    return;
    }
    System.Text.StringBuilder hexstr=new System.Text.StringBuilder(buffer.Length <<1);
    foreach(byte b in buffer)
    hexstr.Append(b.ToString("X2"));this.richTextBox1.Text =hexstr.ToString();System.DateTime t2=System.DateTime.Now;
    System.TimeSpan diff=t2.Subtract(t1);
    MessageBox.Show(diff.ToString());
    MessageBox.Show(hexstr.Length.ToString());
      

  4.   

    如楼上所言,这个问题主要解决读取和转换。文件读取一定要用buffer,这是最快的(从Dos开始就一直如此),转换还是用StringBuilder,还有更快的算法,不过太麻烦了,而且效果提高不了多少。