473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Nov 17 '05 #1
6 8940
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.

"Jozef Jarosciak" <jo*@doprocess.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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

Nov 17 '05 #2
Jozef Jarosciak <jo*@doprocess.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
On 9 Jul 2005 13:57:20 -0700, "Jozef Jarosciak" <jo*@doprocess.com>
wrote:
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
Nov 17 '05 #4
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.

Nov 17 '05 #5
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>

Jozef Jarosciak wrote:
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.

Nov 17 '05 #6

"Jozef Jarosciak" <jo*@doprocess.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Deano | last post by:
I think my brain has short-circuited again :) Is this the quickest way to check for the existence of a given value in an array? e.g For i = 0 To rrst.RecordCount If myArray(i) =...
3
by: Alex Munk | last post by:
I would like to be able to pass a 2-dimensional array to a function as a string parameter. In the called function I would like to be able to convert the string back into a 2-dimensional array....
2
by: Sean | last post by:
Hello, I have a function in C# that accepts a stored proc name and an array of parameters, and then returns a dataset, which works just fine. I have converted the function to VB: Public Shared...
6
by: baret bonden | last post by:
I get :Value of type 'String' cannot be converted to '1-dimensional array of String' refering to curitem Dim curItem As String curItem = ListBox1.SelectedItem TextBox1.Text = curItem ...
3
by: Jozef Jarosciak | last post by:
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...
5
by: Jackson | last post by:
I have something that is stumping me. I am trying to initialize a 3 dimensional string array with the code below, but it wont compile. Can anyone explain what Im doing wrong?????????????? Im...
1
by: trpost | last post by:
I am looking for a way to find the differences between 2 multidimensional arrays. I have found ways to do this based on 1 key, but I want to be able to look at differences based on all keys: For...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.