运行时间太长,无法从txt C#添加和查找字符串 [英] Running time take its too long for adding and finding string from txt C#
本文介绍了运行时间太长,无法从txt C#添加和查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello兄弟,姐姐,好友,程序员,大师。 :) :) :)
根据搜索的运行时间,此代码需要太长时间。
请提供建议高效。
如何更改或改进我的代码:
Hello Brother, Sister, Buddy, Programmer, Master. :) :) :)
Based on running time for searching, this code take its too long.
Please give recommendation for making it efficient.
How to change or improve my code like this :
static List<string> getDBList(string DBname)
{
List<string> listWords = new List<string>();
string[] files;
try
{
files = Directory.GetFiles(@"dbase/", DBname);
foreach (string file in files)
foreach (string line in File.ReadAllLines(file))//doubt
listWords.Add(line.Trim().ToUpperInvariant());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return new List<string> { };
}
return listWords;
}
然后......
Then...
//MAIN PROGRAM
string allInput = rtbInput.Text;
List<string> splitString = new List<string>.Split(new char[] { ' ', '\t', etc...});
List<int> AllIndexes = new List<int>();
HashSet<string> nounList = new HashSet<string>(getDBList("nounList.txt"));//doubt
int startIndexes = 0;
foreach (string s in splitString)
{
if (s.Trim() != "")
{
string word = s.Trim();
if(!(nounList.Contains(word.ToUpperInvariant()))) //doubt, if not found, color it
{
tbTest.Text += word + " ";
//index to begin color the text
AllIndexes = WordsIndex(word, startIndexes);
foreach (int item in AllIndexes) //Coloring all appearance of the word.
{
tbSeeIndex.Text += Convert.ToString(" " + item + " ");
rtbInput.Select(item, word.Length);
startIndexes = item + word.Length;
rtbInput.SelectionColor = Color.Red;
}
tbL.Text += Convert.ToString(" " + startIndexes + " ");
}
}
}
in nounList(90963字),内容如下:
预订
chari
铅笔
桌子
等...
请帮帮我.....
非常感谢。 :) :)干杯
in nounList (90963 word), content like this example :
book
chari
pencil
table
etc...
Please help me.....
Thanks a lot. :) :) :) Cheers
推荐答案
你没有得到这个想法。字典用于速度,是的。但为什么速度?通过唯一键找到一个值的速度,这个搜索的计算时间复杂度是O(1),'是,它渐渐 不依赖于项目数量。
请参阅:
http://en.wikipedia .org / wiki / Computational_complexity_theory [ ^ ],
http://en.wikipedia.org/wiki/Communication_complexity [ ^ ],
http://en.wikipedia.org/wiki/Big_O_notation [ ^ ]。
如果您只是读取文件的行,则可以使用t下摆作为键,作为键,但不作为字典中使用的键值对。如果你的键和值是相同的,它什么都不给你,因为如果你知道它们是相同的并且有一个键,为什么要寻找一个值呢? (但是,它将保证密钥唯一性,但为此目的,您应该使用类System.Collections.Generic.HashSet< T>
。)
这样,你的问题根本就没有意义。如果你解释一下你要用字典做什么(首先,对你自己),我很乐意帮忙。 :-)
-SA
You are not getting the idea. Dictionaries are used for speed, yes. But for speed of what? The speed of finding a value by unique key, the computational time complexity of this search is O(1), that''s is, it asymptotically does not depend on the number of items.
Please see:
http://en.wikipedia.org/wiki/Computational_complexity_theory[^],
http://en.wikipedia.org/wiki/Communication_complexity[^],
http://en.wikipedia.org/wiki/Big_O_notation[^].
If you simply read the lines of a file, you can use them as vales, as keys, but not as key-value pairs used in dictionary. And if your keys and values are the same, it gives you nothing, because if you know they are the same and have a key, why looking for a value? (However, it will guarantee key uniqueness, but for this purpose, you should use the classSystem.Collections.Generic.HashSet<T>
.)
This way, your question simply makes no sense. If you explain what are you going to do with a dictionary (first of all, to yourself), I would gladly help. :-)
—SA
是的,我看不出字典怎么样会有所帮助。
另外,为什么还要为阵列烦恼呢?列表更好了。
如果文件可能非常大,我还建议不要使用ReadAllLines。
如果你想使用ReadAllLines,你是否考虑过制作阵列列表然后将它们组合起来?
要考虑的一件事如果你只有一个文件然后将它迭代到一个List然后回到一个数组是不好的技术。
Yeah, I don''t see how a Dictionary would help with that.
Plus, why bother with an array? Lists are waaaaay better.
I also recommend not using ReadAllLines if the files may be very large.
If you want to use ReadAllLines, have you considered making a List of the arrays and then combining them?
One thing to consider is that if you have only the one file then iterating it into a List then back out to an array is not good technique.
这篇关于运行时间太长,无法从txt C#添加和查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文