二进制搜索和字符串[] [英] BinarySearch and String[]
问题描述
我在文本文件上执行有效的关键字搜索时遇到问题可能
小于100k。我有一个大约200个字符串的关键字列表,我需要搜索文件并标记列表中的所有单词。我试着用/或
进行正则表达式搜索,但速度太慢了。现在我正在努力做一个直接的文本
搜索一个字符串数组但我无法让它工作。以下是我试过的
,我不知道我做错了什么。有人有建议吗?
if(Array.BinarySearch(keys [x],this.Text.Substring(w.start,w.length))> 0)
其中keys [x] []是一个锯齿状的字符串数组。
I''m having problems doing an efficient keyword search on a text file likely
to be smaller than 100k. I have a keyword list of about 200 strings and I
need to search the file and mark all of the words in the list. I tried doing
a Regex search but it was way too slow. Now I''m trying to do a straight text
search on a string array but I can''t get it to work. Below is what I tried
and I''m not sure what I''m doing wrong. Anybody have a suggestion?
if(Array.BinarySearch(keys[x], this.Text.Substring(w.start, w.length)) > 0)
Where keys[x][] is a jagged array of strings.
推荐答案
>我在文本文件
上执行有效的关键字搜索时遇到问题,可能小于100k。我有一个大约200个字符串的关键字列表
我需要搜索文件并标记列表中的所有单词。我尝试进行正则表达式搜索,但速度太慢了。现在我正在尝试对字符串数组进行直接文本搜索,但我无法让它工作。下面
是我试过的,我不知道我做错了什么。有人有建议吗?
likely to be smaller than 100k. I have a keyword list of about 200 strings
and I need to search the file and mark all of the words in the list. I
tried doing a Regex search but it was way too slow. Now I''m trying to do a
straight text search on a string array but I can''t get it to work. Below
is what I tried and I''m not sure what I''m doing wrong. Anybody have a
suggestion?
循环关键字并使用String.IndexOf搜索字符串中每个关键字的出现次数。
string [] myKeywords = new string [] {" fox"," cat"," dog"};
string myString ="快速的棕色狐狸跳过懒狗。;
foreach(myKeywords中的字符串关键字){
if(myString.IndexOf(keyword)> -1) Console.WriteLine(" Found
{0}。",keyword);
}
Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/
Chuck Bowling< ch ********** @ sbcglobal-NO-SPAM.net>写道:
Chuck Bowling <ch**********@sbcglobal-NO-SPAM.net> wrote:
我在文本文件上进行有效的关键字搜索时遇到问题,可能小于100k。我有一个大约200个字符串的关键字列表,我需要搜索文件并标记列表中的所有单词。我试着进行正则表达式搜索,但速度太慢了。现在我正在尝试对字符串数组进行直接文本搜索,但我无法使其工作。以下是我的尝试
我不知道我做错了什么。有人有建议吗?
if(Array.BinarySearch(keys [x],this.Text.Substring(w.start,w.length))> 0)
>其中keys [x] []是一个锯齿状的字符串数组。
I''m having problems doing an efficient keyword search on a text file likely
to be smaller than 100k. I have a keyword list of about 200 strings and I
need to search the file and mark all of the words in the list. I tried doing
a Regex search but it was way too slow. Now I''m trying to do a straight text
search on a string array but I can''t get it to work. Below is what I tried
and I''m not sure what I''m doing wrong. Anybody have a suggestion?
if(Array.BinarySearch(keys[x], this.Text.Substring(w.start, w.length)) > 0)
Where keys[x][] is a jagged array of strings.
BinarySearch只能用于排序数组。如果没有更多信息,很难给出任何
的建议,我很害怕。
-
Jon Skeet - < sk *** @ pobox.com>
http: //www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
BinarySearch will only work with a sorted array. It''s hard to give any
suggestions without a bit more information, I''m afraid.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Chuck Bowling< ch ********** @ sbcglobal-NO-SPAM.net>写道:
Chuck Bowling <ch**********@sbcglobal-NO-SPAM.net> wrote:
我在文本文件上进行有效的关键字搜索时遇到问题
可能小于100k。我有一个大约200个字符串的关键字列表,我需要搜索文件并标记列表中的所有单词。我试过
一个正则表达式搜索,但它太慢了。现在我正在尝试直接搜索字符串数组,但是我无法让它工作。以下是我的尝试
我不知道我做错了什么。有人有建议吗?
if(Array.BinarySearch(keys [x],this.Text.Substring(w.start,w.length))>
0)
其中keys [x] []是一个锯齿状的字符串数组。
I''m having problems doing an efficient keyword search on a text file
likely
to be smaller than 100k. I have a keyword list of about 200 strings and I
need to search the file and mark all of the words in the list. I tried
doing
a Regex search but it was way too slow. Now I''m trying to do a straight
text
search on a string array but I can''t get it to work. Below is what I
tried
and I''m not sure what I''m doing wrong. Anybody have a suggestion?
if(Array.BinarySearch(keys[x], this.Text.Substring(w.start, w.length)) >
0)
Where keys[x][] is a jagged array of strings.
BinarySearch只能用于排序数组。如果没有更多信息,很难给出任何建议,我很害怕。
BinarySearch will only work with a sorted array. It''s hard to give any
suggestions without a bit more information, I''m afraid.
感谢您的回复。
事实证明我有几个问题阻止了应用程序的运行方式(仅供参考)(仅供参考),我正在搜索已排序的数组并且
上面的BinarySearch应该做一个> =比较。)
不幸的是,它仍然不能很快地工作。
解析一个10k文本文件需要大约20秒,并且在1.6ghz机器上标记大约200个关键字。我是b / b
工作几种方式修剪几秒钟但是我有一种感觉
我可能需要寻找一个完全不同的算法,如果我想要任何
显着的性能提升。
Thanks for your reply.
It turns out I had a couple of problems that stopped the app from working
the way it should (FYI, I was doing a search on a sorted array and the
BinarySearch above should be doing a >= comparison).
Unfortunately, it still doesn''t work very fast. It takes about 20 seconds to
parse a 10k text file and mark about 200 keywords on a 1.6ghz machine. I''m
working on a few ways of trimming some seconds off but I have a feeling that
I may need to look for a completely different algorithm if I want any
significant performance gain.
这篇关于二进制搜索和字符串[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!