判断字符串中是否存在的几种方案:string.indexof、string.contains、list.contains、list.any几种方式效率对比

我们在做项目时,可能会遇到这样的需求,比如判断,1,2,3,33,22,123, 中是否存在,3,

var str=",1,2,3,33,22,123,";

一般有几种方式:

1、str.IndexOf(",3,")>=0

2、str.Contains(",3,")

有可能我们不用字符串而用List来存,判断list中是否存在3

var list = str.Split(',').ToList();

3、list.Contains("3");

4、List.Any(t=>t=="3");

接下来就验证一下上面4种方案究竟哪种效率最高?

public void Test()
        {
            var str = "article.add,article.update,article.delete,article.view," +
                "category.add,category.update,category.delete,category.view," +
                "user.add,user.update,user.delete,user.view," +
                "role.add,role.update,role.delete,role.view," +
                "news.add,news.update,news.delete,news.view," +
                "category.add,category.update,category.delete,category.view," +
                "Threading.add,Threading.update,Threading.delete,Threading.view," +
                "System.add,System.update,System.delete,System.view," +
                "Generic.add,Generic.update,Generic.delete,Generic.view," +
                "Collections.add,Collections.update,Collections.delete,Collections.view," +
                "Tasks.add,Tasks.update,Tasks.delete,Tasks.view," +
                "CslApp.add,CslApp.update,CslApp.delete,CslApp.view";
            var list = str.Split(',').ToList();

            var str1 = "Tasks2.view"; //这里查询一个不存在的字符串,这样更费时,如果字符串存在,话费的时间要少些。
            var count = 100000;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //耗时巨大的代码
            for (int i = 0; i < count; i++)
            {
                if (str.Contains(str1)) { }
            }
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            Console.WriteLine("string.contains总共花费{0}ms.", ts.TotalMilliseconds);

            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            //耗时巨大的代码 
            for (int i = 0; i < count; i++)
            {
                if (list.Contains(str1)) { }
            }
            sw2.Stop();
            TimeSpan ts2 = sw2.Elapsed;
            Console.WriteLine("List.Contains总共花费{0}ms.", ts2.TotalMilliseconds);

            Stopwatch sw3 = new Stopwatch();
            sw3.Start();
            //耗时巨大的代码 
            for (int i = 0; i < count; i++)
            {
                if (list.Any(t=>t == str1)) { }
            }
            sw3.Stop();
            TimeSpan ts3 = sw3.Elapsed;
            Console.WriteLine("List.Any总共花费{0}ms.", ts3.TotalMilliseconds);

            Stopwatch sw4 = new Stopwatch();
            sw4.Start();
            //耗时巨大的代码 
            for (int i = 0; i < count; i++)
            {
                if (str.IndexOf(str1)>=0) { }
            }
            sw4.Stop();
            TimeSpan ts4 = sw4.Elapsed;
            Console.WriteLine("string.IndexOf总共花费{0}ms.", ts4.TotalMilliseconds);

        }

  这里分别循环十万次来测试,得到的结果:

可以看出,IndexOf >=0这种方式简直是灾难性的存在,

效率最高的是将字符串转成List在用List.Contains判断,

List.Any查询效率都远不如string.Contains和List.Contains。

下面运行100万次的结果,由于indexof效率实在太差太费时间,就去掉了这个判断,

可见,转换成List在用list.Contains判断效率是最高的。

posted @ 2018-01-25 01:17  虚若影  阅读(6042)  评论(1编辑  收藏  举报