private static void BuildDictionaryOfRequires(Regex exp, Dictionary<string, string> dictionary, DirectoryInfo dir)
{
var i = 0;
var total = dir.EnumerateFiles("*.*", SearchOption.AllDirectories).
Where(x => x.Extension == ".aspx" || x.Extension == ".ascx").Count();
foreach (var item in dir.EnumerateFiles("*.*", SearchOption.AllDirectories).
Where(x => x.Extension == ".aspx" || x.Extension == ".ascx"))
{
#if DEBUG
Stopwatch sw = Stopwatch.StartNew();
#endif
var text = File.ReadAllText(item.FullName);
MatchCollection matches = exp.Matches(text);
foreach (Match match in matches)
{
var matchValue = match.Groups[0].Value;
if (dictionary.ContainsKey(matchValue))
{
dictionary[matchValue] = string.Format("{0},{1}", dictionary[matchValue], item.Name);
}
else
{
dictionary.Add(matchValue, item.Name);
}
}
Console.WriteLine(string.Format("Found matches in {0}.", item.Name));
#if DEBUG
sw.Stop();
Console.WriteLine("Time used (float): {0} ms", sw.Elapsed.TotalMilliseconds);
#endif
Console.WriteLine(string.Format("{0} of {1}", (++i).ToString(), total));
}
}
there are about 232 files the lambda finds. It rips through 160 just fine then comes to a crawl. I’m profiling the code now but wondering if there is anything obvious i’m doing wrong.
the regex is
Regex exp = new Regex(@"dojo\.require\([""'][\w\.]+['""]\);?", RegexOptions.IgnoreCase | RegexOptions.Compiled);
all of the files are similar length and similar structure.
most files take less than 30ms but some are 11251 ms.
with updated regex the whole process takes 1700ms now. phew!
Try simplifying your regex:
UPDATE: Then remove the semi-colon at the end if you want to match your example.