Hi Alan,
Thanks for your feedback.
First, I want to inform you that ArrayList.Binar ySearch method only can
apply to an array that has been sorted. The entire sort is a heavy
operation for you. So it does not meet your need. The below sample code
cost about 2.3~2.5 seconds, which is poor performance:
ArrayList al=new ArrayList();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i+ +)
{
no=r.Next(10000 0);
al.Add("abcdef" +no);
if(i==50000)
{
searchno=no;
}
}
DateTime dt=DateTime.Now ;
al.Sort();
int pos=-1;
pos=al.BinarySe arch("abcdef"+s earchno);
TimeSpan ts=DateTime.Now-dt;
MessageBox.Show (ts.ToString()) ;
Then, for doing personal search in ArrayList, it costs about 0.25 seconds,
like this:
ArrayList al=new ArrayList();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i+ +)
{
no=r.Next(10000 0);
al.Add("abcdef" +no);
if(i==50000)
{
searchno=no;
}
}
DateTime dt=DateTime.Now ;
string str;
int resultno=-1;
TimeSpan ts=new TimeSpan(0);
for(int pos=0;pos<al.Co unt;pos++)
{
if(al[pos].Equals("abcdef "+searchno) )
{
resultno=pos;
ts=DateTime.Now-dt;
break;
}
}
MessageBox.Show (ts.ToString()) ;
At last, if you use Hashtable.Conta insValue, it will be fastest, about 0.03
seconds, like this:
Hashtable ht=new Hashtable();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i+ +)
{
no=r.Next(10000 0);
ht.Add(i, "abcdef"+no );
if(i==50000)
{
searchno=no;
}
}
DateTime dt=DateTime.Now ;
bool b=ht.ContainsVa lue("abcdef"+se archno);
TimeSpan ts=DateTime.Now-dt;
MessageBox.Show (ts.ToString()) ;
So if you only want to know if certain item existed in a collection, the
Hashtable may be good for you. But it does not give you a definitely
position number in the collection. If you want the exactly position number,
you still have to do the compare yourself.
All the test is on my machine, which performance is not very well :-)
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.