Quickest way to find the string in 1 dimensional string array!

  • Thread starter Jozef Jarosciak
  • Start date
J

Jozef Jarosciak

Quickest way to find the string in 1 dimensional string array!

I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?

This code is in vb.net. But I need a way to do this in C# as well.

Dim queue(1000000) As String
Dim lookfor as String
Dim count, isthere as Integer

While queue(count) <> Nothing
If queue(count) = lookfor Then
isthere = 1
End If
listing2 = count + 1
End While

If isthere = 1 then messagebox.show("Found it!")
else messagebox.show("Didn't Find it!")
End if


Please let me know.
Joe
 
J

Jon Skeet [C# MVP]

Jozef Jarosciak said:
Quickest way to find the string in 1 dimensional string array!

I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?

Use Array.IndexOf in this case. In general though, to exit a loop
early, use break.
 
B

Bob Powell [MVP]

Rather than a 1 dimensional array you should probably be storing your
strings in a hashtable using the generated hash of the string as the key.

The hashtable will give a much faster result for match or no-match on any
given string.

Knowing what this was really for would also enable people to suggest a more
efficient method.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
R

rossum

Quickest way to find the string in 1 dimensional string array!

I have a queue 1 dimensional array of strings called 'queue' and I need
a fast way to search it. Once there is match, I don't need to search
any longer.
Currently I am using this code.
But I think it's too slow, because it runs through whole dimension.
I know this is trivial question, but is there any way to stop this
loop, or better way to search? I mean - FASTER?

This code is in vb.net. But I need a way to do this in C# as well.

Dim queue(1000000) As String
Dim lookfor as String
Dim count, isthere as Integer

While queue(count) <> Nothing
If queue(count) = lookfor Then
isthere = 1
End If
listing2 = count + 1
End While

If isthere = 1 then messagebox.show("Found it!")
else messagebox.show("Didn't Find it!")
End if


Please let me know.
Joe

An alternative:

Use a sorted array instead of your queue. Use binary search to look
for the target string.

As Bob said, we need more information to help determine what solutin
might work best for you.

rossum



The ultimate truth is that there is no ultimate truth
 
J

Jozef Jarosciak

I am building a web crawler. Soon I will release the code under GNU.
Basically, as I am crawling the pages I need to check if new url is
already in the list.
So on small sites, this would work fine. But on the big sites where
there might be well over 1.000.000 urls, it could be a slow process.
I hope this helps to determine the best solution.
So far, thanks for all your help. I am looking forward for new ideas.
 
R

Rodger Constandse

Hi,

Bob's advise of a hashtable would be a good choice for this type of task.

Hashtable gives you fast add and access operations.

You can use the StringDictionary class in System.Collections.Specialized, which
handles the strings in a case insensitive matter. I think this is what you want
for URLs, but I'm not 100% sure?? If you need case sensitive comparisons, just
use the regular Hashtable class.

StringDictionary visitedURLs = new StringDictionary();

....

// assuming url contains the URL to check...

if (!visitedURLs.ContainsKey(url))
{
// add url to visited hashtable
visitedURLs.Add(url);

// take appropriate action for URL's that have not been visited
}
// otherwise, URL has already been visited

Hope this helps.

Best regards,

Rodger

Achieve Planner - Keep track of your projects/tasks and schedule them in your
calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
D

Daniel O'Connell [C# MVP]

Jozef Jarosciak said:
I am building a web crawler. Soon I will release the code under GNU.
Basically, as I am crawling the pages I need to check if new url is
already in the list.
So on small sites, this would work fine. But on the big sites where
there might be well over 1.000.000 urls, it could be a slow process.
I hope this helps to determine the best solution.
So far, thanks for all your help. I am looking forward for new ideas.

Since you are dealing with URLs, which usually describe hiearchys, you might
be able to split the URL up into components and use a tree structure. It'd
likely be a little slower on small sites but chances are it would scale much
better than an array would. It'd also save space as you should be able to
use string interning to your advantage.

for a URL like http://host/folder/file you would look for "host" in the
first level of nodes, "folder" in the second, and "file" in the third.

I'm not sure it'd work, but I think it will and it's certainly worth a
little research.
 
M

Michael S

Jozef Jarosciak said:
Dim queue(1000000) As String
Dim lookfor as String
Dim count, isthere as Integer

While queue(count) <> Nothing
If queue(count) = lookfor Then
isthere = 1
End If
listing2 = count + 1
End While

If isthere = 1 then messagebox.show("Found it!")
else messagebox.show("Didn't Find it!")
End if

Is it just me who have noticed, but that doesn't look like C# to me.
Why do we keep helping dudes that has no respect when it comes to where they
post?

Well, I won't do it no more. Netiquette!
- Michael S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top