日期:2014-05-20  浏览次数:20773 次

[大文件中查找字符串],我的C#代码比同事的C++慢5倍左右……郁闷
请教诸位高手,下面的代码速度可否再有优化?            

Stopwatch   sw   =   new   Stopwatch();
output( "计算开始... ");
sw.Start();
using   (StreamReader   sr   =   new   StreamReader(tb_filepath.Text))
        {
                String   line;
                while   ((line   =   sr.ReadLine())   !=   null)
                {
                      if   (line.IndexOf(tb_keyword.Text)   !=   -1)   {   count++;   };
                }
                output( "字符串出现次数: "+count.ToString());
        }
sw.Stop();
decimal   micro   =   sw.Elapsed.Ticks   /   10000000m;
output( "计算结束 ");
output( "花费时间: "+micro.ToString()+ "   s ");

------解决方案--------------------
//我怀疑是不是 StreamReader.ReadLine()返回String对象过程中 box unbox造成的?
根本不存在box unbox的问题
楼主看一下我的回复
------解决方案--------------------
为 tb_keyword.Text 定义一个临时变量,会快一些。