zoukankan      html  css  js  c++  java
  • 字符串非空判断的效率问题

       static void TestString()
            {
                string[] allstring = File.ReadAllLines(Path.Combine(AppContext.BaseDirectory, "text.txt"));
                int i=0,j = 0;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                foreach (var item in allstring)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        i++;
                    }
                    else
                    {
                        j++;
                    }
                }
                sw.Stop();
                Console.WriteLine($"IsNullOrWhiteSpace方法  总共{allstring.Length}数据,非空 {i++} 条,空 {j},耗时:{sw.ElapsedMilliseconds}");
            }
            static void TestString2()
            {
                string[] allstring = File.ReadAllLines(Path.Combine(AppContext.BaseDirectory, "text.txt"));
                int i = 0, j = 0;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                foreach (var item in allstring)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        i++;
                    }
                    else
                    {
                        j++;
                    }
                }
                sw.Stop();
                Console.WriteLine($"IsNullOrEmpty方法  总共{allstring.Length}数据,非空 {i++} 条,空 {j},耗时:{sw.ElapsedMilliseconds}");
            }
    
            static void TestString1()
            {
                string[] allstring = File.ReadAllLines(Path.Combine(AppContext.BaseDirectory, "text.txt"));
                int i = 0, j = 0;
                Stopwatch sw = new Stopwatch();
                sw.Start();
                foreach (var item in allstring)
                {
                    if (item!=null&&item.Length>0)
                    {
                        i++;
                    }
                    else
                    {
                        j++;
                    }
                }
                sw.Stop();
                Console.WriteLine($"==null length>0 方法  总共{allstring.Length}数据,非空 {i} 条,空 {j},耗时:{sw.ElapsedMilliseconds}");
            }
    

      本文主要测试三种判断字符串非空的方法。

    1.IsNullOrWhiteSpace  是否为null或空格字符。

    2.用==null && Length>0判断字符串是否为空。

    3.IsNullOrEmpty 判断字符是否为null或Empty。

    数据量

    100条内。

     万条内

     一万条

     10万条

     100万条

     500万条 带空格字符

     500万条 不带空格字符

     1500万条 带有空格字符。

    结论:

    1.数量少的时候你不需要纠结。三个哪个顺手就用哪个。

    2.数据量大的时候很明显 IsNullOrEmpty 是比较优秀的。

    3.如果要排除空格字符的话 还是要用 IsNullOrWhiteSpace。

  • 相关阅读:
    Java快速开发框架LML功能菜单管理
    性能测试知多少性能需求分析
    C++学习的方法以及四大名著
    .NET Micro Framework官方库+Controller Area Network
    PostgreSQL学习笔记
    幻风阁|kent.zhu'sBlog对google阅读器共享设计的再讨论
    【数据库事务日志碎片原理分析与方案】分析篇
    Windows 8 RC升级到RTM很顺利
    http://www.cnblogs.com/oneday/archive/2012/08/16/2643039.html
    Visual Studio 2012 Ultimate RTM 体验
  • 原文地址:https://www.cnblogs.com/lecone/p/12516424.html
Copyright © 2011-2022 走看看