【发布时间】:2017-04-12 17:08:10
【问题描述】:
我有一个项目可以读取 100 个文本文件,其中包含 5000 个单词。
我将单词插入到列表中。我有第二个列表,其中包含英语停用词。我比较两个列表并从第一个列表中删除停用词。
运行应用程序需要 1 小时。我想并行化它。我该怎么做?
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
List<string> listt1 = new List<string>();
string line;
for (int ii = 1; ii <= 49; ii++)
{
string d = ii.ToString();
using (StreamReader reader = new StreamReader(@"D" + d.ToString() + ".txt"))
while ((line = reader.ReadLine()) != null)
{
string[] words = line.Split(' ');
for (int i = 0; i < words.Length; i++)
{
listt1.Add(words[i].ToString());
}
}
listt1 = listt1.ConvertAll(d1 => d1.ToLower());
StreamReader reader2 = new StreamReader("stopword.txt");
List<string> listt2 = new List<string>();
string line2;
while ((line2 = reader2.ReadLine()) != null)
{
string[] words2 = line2.Split('\n');
for (int i = 0; i < words2.Length; i++)
{
listt2.Add(words2[i]);
}
listt2 = listt2.ConvertAll(d1 => d1.ToLower());
}
for (int i = 0; i < listt1.Count(); i++)
{
for (int j = 0; j < listt2.Count(); j++)
{
listt1.RemoveAll(d1 => d1.Equals(listt2[j]));
}
}
listt1=listt1.Distinct().ToList();
textBox1.Text = listt1.Count().ToString();
}
}
}
}
【问题讨论】:
-
如果运行时间那么长,就会出现问题。
-
我用两个文件做的,列表计数是 1780 现在我用 49 个文件做了,它运行了 35 分钟
-
运行两个文件需要多长时间?
-
大约需要 3 分钟
-
但文件字数不同,第一个有 1500 个字,第二个有 5000 个字
标签: c# multithreading file text parallel-processing