您的位置:程序门 -> .net技术 -> c#



有什么字符串数组的高效使用方法吗?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


有什么字符串数组的高效使用方法吗?[已结贴,结贴人:mqmmx]
发表于:2007-09-18 14:58:35 楼主
大至需求如下:
有一大堆字符串,需加入一个字符串数组,如需加入的字符串已存在数组中则返回所在数组的索引位置,如不存在则加入后再返回索引位置。
如下面的代码速度不是很快,有没有更好的,网上说把字符串数组连接为字符串加正则来做会快但好像没办法用在这里,不知有谁能写出更快的,数组大至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;
}
发表于:2007-09-18 15:01:141楼 得分:5
学习,关注一下
发表于:2007-09-18 15:05:142楼 得分:5
@_@
发表于:2007-09-18 15:09:223楼 得分:40
直接使用hashtable   。这样会更快一些!
发表于:2007-09-18 15:12:084楼 得分:5
关注
发表于:2007-09-18 15:13:505楼 得分:40
hashtabele试试
发表于:2007-09-18 15:14:116楼 得分:5
短一点,不知道快不快  
                      string   b= "aa ";
                        list <string>   a   =   new   list <string> ();
                        if   (!a.contains(b))
                                a.add(b);
                        return   a.indexof(b);
                       
发表于:2007-09-18 15:33:027楼 得分:0
xray2005(风车车--要飞翔,必须靠自己!)   你的方法要循环两次不会快吧?(a.contains(b)一次indexof一次)

bearrui(ak-47)用hashtable如何得到要查字串的索引号,要用循环吗?有没有代码可参考?
发表于:2007-09-18 15:40:058楼 得分:0
试了一下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);
                }
        }
发表于:2007-09-18 15:43:209楼 得分:0
楼上各位我要的索引会加入其它处理,所以用hashtable好像不太方便
发表于:2007-09-18 15:49:4210楼 得分:100
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

用空间换时间
用一个排序的列表充当索引
发表于:2007-09-18 15:52:5811楼 得分:0
在我机器上(2*1.6g)
也就是用楼主的方法插入十万次需要16秒
而通过索引插入只需要不到1秒的时间。
发表于:2007-09-18 15:56:4812楼 得分:0
多谢楼上,我用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;
                }
        }
发表于:2007-09-18 15:58:5413楼 得分:0
hashtable好像是快一些,我比较贪心,有没有更快的?
发表于:2007-09-18 16:09:5114楼 得分:0
嗯hash更快一些,当然还是空间换时间的原则
目前的处理速度再优化也提高不了多少
再看看有没有人继续-_-!!!


快速检索

最新资讯
热门点击