大至需求如下:
有一大堆字符串,需加入一个字符串数组,如需加入的字符串已存在数组中则返回所在数组的索引位置,如不存在则加入后再返回索引位置。
如下面的代码速度不是很快,有没有更好的,网上说把字符串数组连接为字符串加正则来做会快但好像没办法用在这里,不知有谁能写出更快的,数组大至int.MaxValue的长度
List<string> stringcollection = new List<string>();
int AddString(string st)
{
int tmp = stringcollection.IndexOf(st);
if ( tmp >-1)
   return tmp;stringcollection.Add(st);
return stringcollection.count-1;
}

解决方案 »

  1.   

    直接使用hashtable 。这样会更快一些!
      

  2.   

    短一点,不知道快不快 
               string b="aa";
                List<string> A = new List<string>();
                if (!A.Contains(b))
                    A.Add(b);
                return A.IndexOf(b);
                
      

  3.   

    xray2005(风车车--要飞翔,必须靠自己!) 你的方法要循环两次不会快吧?(A.Contains(b)一次IndexOf一次)BearRui(AK-47)用hashtable如何得到要查字串的索引号,要用循环吗?有没有代码可参考?
      

  4.   

    试了一下xray2005(风车车--要飞翔,必须靠自己!) 的方法果然不乍地
        class Program
        {
            static void Main(string[] args)
            {
                DateTime dt = System.DateTime.Now;
                string st = "";
                Random rd = new Random();            for (int i = 0; i < 100000; i++)
                {
                    st = ((char)rd.Next(65, 117)).ToString() + ((char)rd.Next(65, 117)).ToString();
                    AddString(st);
                }
                TimeSpan ts = System.DateTime.Now - dt;
                Console.WriteLine(ts);            dt = System.DateTime.Now;
                for (int i = 0; i < 100000; i++)
                {
                    st = ((char)rd.Next(65, 117)).ToString() + ((char)rd.Next(65, 117)).ToString();
                    GetString(st);
                }
                ts = System.DateTime.Now - dt;
                Console.WriteLine(ts);        }        static List<string> stringcollection = new List<string>();
            static int AddString(string st)
            {
                int tmp = stringcollection.IndexOf(st);
                if (tmp > -1)
                    return tmp;            stringcollection.Add(st);
                return stringcollection.Count - 1;        }        static int GetString(string st)
            {
                if (!stringcollection.Contains(st))
                    stringcollection.Add(st);
                return stringcollection.IndexOf(st);
            }
        }
      

  5.   

    楼上各位我要的索引会加入其它处理,所以用Hashtable好像不太方便
      

  6.   

    List<string> stringcollection = new List<string>();
    int AddString1(string st)
    {
        int tmp = stringcollection.IndexOf(st);
        if (tmp > -1)
            return tmp;    stringcollection.Add(st);
        return stringcollection.Count - 1;
    }SortedList<string, int> sortedList = new SortedList<string, int>();
    int AddString2(string st)
    {
        int tmp = sortedList.IndexOfKey(st);
        if (tmp > -1)
            return sortedList.Values[tmp];
        sortedList.Add(st, stringcollection.Count);
        stringcollection.Add(st);
        return stringcollection.Count - 1;
    }private void button1_Click(object sender, EventArgs e)
    {
        Random vRandom = new Random(10010); // 保证随机数相同
        stringcollection.Clear();
        int vTickCount = Environment.TickCount;
        for (int i = 0; i < 100000; i++) 
        {
            AddString1(vRandom.Next(10000).ToString());
        }
        Console.WriteLine("100000次AddString1()消耗{0} 个数:{1}", 
            Environment.TickCount - vTickCount, stringcollection.Count);    vRandom = new Random(10010); // 保证随机数相同
        stringcollection.Clear();
        sortedList.Clear(); // 清除字典数据
        vTickCount = Environment.TickCount;
        for (int i = 0; i < 100000; i++)
        {
            AddString2(vRandom.Next(10000).ToString());
        }
        Console.WriteLine("100000次AddString2()消耗{0} 个数:{1}",
            Environment.TickCount - vTickCount, stringcollection.Count);
    }结果
    100000次AddString1()消耗16266 个数:9999
    100000次AddString2()消耗641 个数:9999用空间换时间
    用一个排序的列表充当索引
      

  7.   

    多谢楼上,我用hashtable好像更快
        class Program
        {
            static void Main(string[] args)
            {
                string st = "";
                Random rd = new Random();
                DateTime dt = System.DateTime.Now;            for (int i = 0; i < 100000; i++)
                {
                    st = ((char)rd.Next(65, 117)).ToString() + ((char)rd.Next(65, 117)).ToString();
                    AddString2(st);
                }
                TimeSpan ts = System.DateTime.Now - dt;
                Console.WriteLine(ts);            dt = System.DateTime.Now;
                for (int i = 0; i < 100000; i++)
                {
                    st = ((char)rd.Next(65, 117)).ToString() + ((char)rd.Next(65, 117)).ToString();
                    GetString(st);
                }
                ts = System.DateTime.Now - dt;
                Console.WriteLine(ts);        }
            static int hs = 0;        static Hashtable ht = new Hashtable();
            static List<string> stringcollection = new List<string>();
            static int AddString(string st)
            {
                int tmp = stringcollection.IndexOf(st);
                if (tmp > -1)
                    return tmp;            stringcollection.Add(st);
                return stringcollection.Count - 1;
            }        static int GetString(string st)
            {
                if (ht.ContainsKey(st))
                    return Convert.ToInt32(ht[st]);            ht.Add(st, hs);
                hs++;
                return hs - 1;
            }
            static SortedList<string, int> sortedList = new SortedList<string, int>();
            static int AddString2(string st)
            {
                int tmp = sortedList.IndexOfKey(st);
                if (tmp > -1)
                    return sortedList.Values[tmp];
                sortedList.Add(st, stringcollection.Count);
                stringcollection.Add(st);
                return stringcollection.Count - 1;
            }
        }
      

  8.   

    hashtable好像是快一些,我比较贪心,有没有更快的?
      

  9.   

    嗯hash更快一些,当然还是空间换时间的原则
    目前的处理速度再优化也提高不了多少
    再看看有没有人继续-_-!!!