加速解析算法

ale*_*lex 4 c# algorithm refactoring parsing

我正在尝试解析一些ddump文件,你能帮我加速算法吗?
每个循环需要216毫秒!! 这太过分了.我希望每循环大约40-50毫秒.也许通过使用RegExp?

这是我的algrithm:

 while (pos < EntireFile.Length && (/*curr = */EntireFile.Substring(pos, EntireFile.Length - pos)).Contains(" class"))
            {
                w.Reset();
                w.Start();
                pos = EntireFile.ToLower().IndexOf(" class", pos) + 6;
                int end11 = EntireFile.ToLower().IndexOf("extends", pos);
                if (end11 == -1)
                    end11 = EntireFile.IndexOf("\r\n", pos);
                else
                {
                    int end22 = EntireFile.IndexOf("\r\n", pos);
                    if (end22 < end11)
                        end11 = end22;
                }
                //string opcods = EntireFile.Substring(pos, EntireFile.Length - pos);
                string Cname = EntireFile.Substring(pos, end11 - pos).Trim();
                pos += (end11 - pos) + 7;
                pos = EntireFile.IndexOf("{", pos) +1;

int count = 1; string searching = EntireFile.Substring(pos, EntireFile.Length - pos); int searched = 0; while (count != 0) { if (searching[searched] == '{') count++; else if (searching[searched] == '}') count--; searched++; } string Content = EntireFile.Substring(pos, searched); tlist.Add(new TClass() { ClassName = Cname, Content = Content }); pos += searched; if (pos % 3 == 0) { double prc = ((double)pos) * 100d / ((double)EntireFile.Length); int prcc = (int)Math.Round(prc); wnd.UpdateStatus(prcc); wnd.Update(); } mils.Add((int)w.ElapsedMilliseconds); }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Jon*_*Jon 9

好吧,多次这样做

EntireFile.ToLower()
Run Code Online (Sandbox Code Playgroud)

当然不会有帮助.你可以做几件事:

  1. 仅执行一次昂贵的操作(ToLower,IndexOf等),并尽可能缓存结果.
  2. 不要缩小您正在处理的输入SubString,这将会影响您的性能.而是保留一个单独的int parseStart值,并将其用作所有IndexOf调用的附加参数.换句话说,跟踪手动解析的文件部分,而不是每次都使用较小的子字符串.