• 判断字符串中是否存在的几种方案: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判断效率是最高的。

  • 相关阅读:
    结对项目电梯调度--设计模拟
    程序的单元测试
    一个文本单词统计的案例
    MFC vs2012 Office2013 读写excel文件
    Unix NetWork Programming(unix环境编程)——环境搭建(解决unp.h等源码编译问题)
    VMware三种上网模型
    矩阵求逆算法及程序实现(C++)
    unix环境高级编程基础知识之第四章
    2014阿里研发面试题目
    MFC下debug改成release版本出现问题及解决办法
  • 原文地址:https://www.cnblogs.com/hpnet/p/8345632.html
Copyright © 2020-2023  润新知