为什么这个循环运行得这么慢:c#
因此,我正在从事一个文本挖掘项目,目前正试图实现信息增益。我有一个数据,其中每行描述一个文档。因此,新行字符可以分割不同的文档 我必须生成一个矩阵,其中列是所有文档中所有不同的单词,行是不同的文档。对于文档中是否存在单词,此表中的每个单元格为1(true)或0(false)。 共有987份文件,总字数为22860,不同字数为3680。因此,将3680个单词与22860个进行比较。这是运行缓慢,但我很好。当我遍历单词列表中的对象以生成矩阵时,需要花费更多时间的循环。见下文 注意:我已经删除了文档中所有重复的单词为什么这个循环运行得这么慢:c#,c#,performance,list,loops,C#,Performance,List,Loops,因此,我正在从事一个文本挖掘项目,目前正试图实现信息增益。我有一个数据,其中每行描述一个文档。因此,新行字符可以分割不同的文档 我必须生成一个矩阵,其中列是所有文档中所有不同的单词,行是不同的文档。对于文档中是否存在单词,此表中的每个单元格为1(true)或0(false)。 共有987份文件,总字数为22860,不同字数为3680。因此,将3680个单词与22860个进行比较。这是运行缓慢,但我很好。当我遍历单词列表中的对象以生成矩阵时,需要花费更多时间的循环。见下文 注意:我已经删除了文档中
class word_list
{
public string word;
public List<bool> doc= new List<bool>();
};//class ends
private void button2_Click(object sender, EventArgs e)
{
//Convert the string into an array of words
string[] w1 = richTextBox1.Text.Trim().Split('\n',' ').Select(x => x.Trim().ToLower()).Distinct().ToArray(); //all distinct words
string[] rich_doc = richTextBox1.Text.Trim().Split('\n'); //all documents array
List<word_list> words = new List<word_list>();
richTextBox2.Text+=("no. of distict words: " + w1.Length + ", no. of docs " + rich_doc.Length);
for (int i = 0; i < w1.Length; i++)
{
word_list temp = new word_list();
temp.word = w1[i]; //temp has the current distict word as class object
for(int j=0;j<rich_doc.Length;j++)//traverse all doc array
{
temp.doc.Add(false);
List<string> doc_word = Regex.Split(rich_doc[j], @"\b").Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
//richTextBox2.Text += ("\n no. of words in this doc: " + doc_word.Count);
//richTextBox2.SelectionStart = richTextBox1.Text.Length;
//richTextBox2.Focus();
int doc_count = doc_word.Count; // number of docs
for (int k = 0; k < doc_count; k++)//All words in a doc are compared
{
if(doc_word[k].ToLower() == w1[i].ToLower())
{
temp.doc[temp.doc.Count-1]=true;
break;
}
}
}
if ((words.Count - 1)>=0)
richTextBox2.Text += ("\n word(" + words.Count + "/" + w1.Length + "): " + words[words.Count - 1].word);
richTextBox2.SelectionStart = richTextBox1.Text.Length;
richTextBox2.Focus();
words.Add(temp);
}
//generate matrix
int t = rich_doc.Length; //no. of docs
int word_count = words.Count;
richTextBox1.Text = "Doc";
foreach (word_list w in words)
{
richTextBox1.Text += "\t" + w.word;
}
richTextBox1.Text += "\n";
//This loop is slow
for (int i = 0; i < t; i++) //traverse through number of docs
{
richTextBox1.Text += i + 1;
for (int h = 0; h < word_count; h++)//traverse through each distinct word in the list
{
if (words[h].doc[i])
richTextBox1.Text += "\t1";
else
richTextBox1.Text += "\t0";
}
richTextBox1.Text += "\n";
}
}//end of button 2
类单词列表
{
公共字符串;
公共列表单据=新列表();
};//下课
私有无效按钮2\u单击(对象发送者,事件参数e)
{
//将字符串转换为单词数组
字符串[]w1=richTextBox1.Text.Trim().Split('\n','')。选择(x=>x.Trim().ToLower()).Distinct().ToArray();//所有不同的单词
字符串[]rich_doc=richTextBox1.Text.Trim().Split('\n');//所有文档数组
列表单词=新列表();
richTextBox2.Text+=(“区分字数:“+w1.Length+”,文档数“+rich_doc.Length”);
对于(int i=0;i
ta.speot.is是正确的。字符串应该使用构建,例如使用,并且仅在循环后将字符串指定给richTextBox1.Text
。代码如下所示:
//generate matrix
StringBuilder sb = new StringBuilder();
int t = rich_doc.Length; //no. of docs
int word_count = words.Count;
richTextBox1.Text = "Doc";
foreach (word_list w in words)
{
sb.Append("\t");
sb.Append(w.word);
}
sb.AppendLine();
//This loop is not slow anymore :)
for (int i = 0; i < t; i++) //traverse through number of docs
{
sb.Append(i + 1);
for (int h = 0; h < word_count; h++)//traverse through each distinct word in the list
{
if (words[h].doc[i])
sb.Append("\t1");
else
sb.Append("\t0");
}
sb.AppendLine();
}
richTextBox1.Text = sb.ToString();
//生成矩阵
StringBuilder sb=新的StringBuilder();
int t=丰富的文档长度//文件数目
int word_count=单词数;
richTextBox1.Text=“Doc”;
foreach(单词列表w,大写)
{
某人追加(“\t”);
某人附加(w.word);
}
(某人);
//此循环不再慢:)
for(int i=0;i
编辑:下面有一些有价值的评论。更改属性是这里最昂贵的操作。mediafire.com/?4mojnj4j153q76s:这是我正在处理的数据,richtextbox2用于测试目的考虑使用
StringBuilder
来构建字符串,而不是使用TextBox.Text
作为工作空间。是否立即试用StringBuilder
是与不使用richTextBox1.Text
相比,这可能是一个微不足道的优化。设置Text
将启动各种各样的处理。他使用string+=
可能没有使用richTextBox.Text+=
那么痛苦。)当然,字符串添加比StringBuilder.Append慢得多,但是设置文本和在屏幕上中继/重画文本更慢!(虽然RTBox可能不会立即重新绘制,但它仍然会强制无效和许多不必要的重新绘制。)@quetzalcatl是的,我也注意到了同样多的内容。@ta.speot.is-是的,我看到你以10秒的优势击败了我:)但实际上我想提示辩证法在他的回答中包含实际问题。