private void profileTyposToolStripMenuItem_Click(object sender, EventArgs e)
{
#if DEBUG
if (RegexTypos == null)
{
MessageBox.Show("No typos loaded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
List<KeyValuePair<Regex, string>> typos = RegexTypos.GetTypos();
if (!typos.Any())
{
MessageBox.Show("No typos loaded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string text = txtEdit.Text;
if (!txtEdit.Enabled || text.Length == 0)
{
MessageBox.Show("No article text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (MessageBox.Show("Test typo rules for performance (this takes up to 5 minutes)?",
"Test typos", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
return;
int iterations = 1000000 / text.Length;
if (iterations > 500) iterations = 500;
List<KeyValuePair<int, string>> times = new List<KeyValuePair<int, string>>();
foreach (KeyValuePair<Regex, string> p in typos)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++)
{
p.Key.IsMatch(text);
}
times.Add(new KeyValuePair<int, string>((int)watch.ElapsedMilliseconds, p.Key + " > " + p.Value));
}
times.Sort(CompareRegexPairs);
StringBuilder builder = new StringBuilder();
builder.AppendLine("Profiling " + iterations + @" iterations of """ + TheArticle.Name + @"""");
foreach (KeyValuePair<int, string> p in times) builder.AppendLine(p.ToString());
Tools.WriteTextFile(builder, "typos.txt", false);
MessageBox.Show("Results are saved in the file 'typos.txt'", "Profiling complete",
MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
}