I’m trying to parse some ddump files, could you please help me speed up my algorithm?
It takes 216 ms for each loop!! that is way too much. I would like to have it around 40-50 ms per loop. Maybe by using RegExp?
Here is my 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);
}
Any help would be greatly appreciated.
Well, doing this multiple times
certainly will not help. There are several things you can do:
ToLower,IndexOf, etc) only once and cache the results if possible.SubString, this will kill your performance. Rather, keep a separateint parseStartvalue and use that as an additional parameter to all of yourIndexOfcalls. In other words, keep track of the part of the file you have parsed manually instead of taking a smaller substring each time.