borked technique Posted February 27, 2006 Share Posted February 27, 2006 Is there a fast way to search for a string in c#? or at least be able to use ststr from c? I have to go through a humongo loop and it takes forever with indexof. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/ Share on other sites More sharing options...
0 John Veteran Posted February 27, 2006 Veteran Share Posted February 27, 2006 What's your loop look like? What makes you think IndexOf() is slowing you down? Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587250661 Share on other sites More sharing options...
0 THE BAT Posted February 28, 2006 Share Posted February 28, 2006 John said: What makes you think IndexOf() is slowing you down? Yes, why you think that ? Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587251632 Share on other sites More sharing options...
0 gigapixels Veteran Posted February 28, 2006 Veteran Share Posted February 28, 2006 IndexOf() is pretty standard as far as I've seen. I too would like to know why you think it's slowing you down... Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587252961 Share on other sites More sharing options...
0 azcodemonkey Posted February 28, 2006 Share Posted February 28, 2006 borked technique said: Is there a fast way to search for a string in c#? or at least be able to use ststr from c? I have to go through a humongo loop and it takes forever with indexof. Is your string made up of multiple words? Does it change much? You could split the words out into an array, do a sort on it, and then use the BinarySearch feature of the Array object to find your word. It really depends on the string you are searching. If it doesn't change much, then cache the split and sorted string array for searching. If the string changes a lot, then it might be faster to use this approach over the loop/indexOf, but I've never done a timed comparison. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587253997 Share on other sites More sharing options...
0 borked technique Posted February 28, 2006 Author Share Posted February 28, 2006 i was reading a 600 mb text file. Reading a line and using indexof to see if the line contained a string took over two and a half minutes at full CPU. I wrote my own comparison and it took 15 seconds at 70% CPU. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587255012 Share on other sites More sharing options...
0 _kane81 Posted February 28, 2006 Share Posted February 28, 2006 could you post your code for your comparison function. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587255457 Share on other sites More sharing options...
0 John Veteran Posted March 1, 2006 Veteran Share Posted March 1, 2006 Well I don't even want to imagine what kind of performance a 600 mb string object would have. Split it up if you can (Y) But since you say you've created something to do it in 15 seconds, you've caught my attention; what did you do? Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587256942 Share on other sites More sharing options...
0 borked technique Posted March 1, 2006 Author Share Posted March 1, 2006 I use a streamreader and streamwriter to read a line of text and write a line of text. I do a ToCharArray() on the string i am searching for and searching in before calling my search. Code below: public void searchString(char[] sfor, char[] sin) { int i = 0; int j = 0; found = 0; while (j < sin.Length - sfor.Length + 1) { if (i == sfor.Length) { found = 1; break; } if (sfor == sin) { i++; } else { j++; i = 0; } } } Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587257984 Share on other sites More sharing options...
0 Antaris Veteran Posted March 1, 2006 Veteran Share Posted March 1, 2006 Don't forget, I think IndexOf() is a linear search algorithm, so the time it takes to find the data is comparable to the length of the data you are entering. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587258023 Share on other sites More sharing options...
0 borked technique Posted March 1, 2006 Author Share Posted March 1, 2006 My code is pretty much linear as well. Link to comment https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/#findComment-587258153 Share on other sites More sharing options...
Question
borked technique
Is there a fast way to search for a string in c#? or at least be able to use ststr from c? I have to go through a humongo loop and it takes forever with indexof.
Link to comment
https://www.neowin.net/forum/topic/437182-c-string-search-indexof-too-slow/Share on other sites
10 answers to this question
Recommended Posts