C# 判断字符为空的6种方法的效率实测对比

所属分类: 软件编程 / C#教程 阅读数: 48
收藏 0 赞 0 分享

C#中提供了相当丰富的方法或属性来判断一个字符是否为空,常用的方法有以下6种

1. strTest== ""

2. strTest.Equals("")

3. strTest== string.Empty

4. strTest.Equals(string.Empty)

5. strTest.Length == 0

6. string.IsNullOrEmpty(strTest)

为了对以上6种方法的效率,有个直观的感受,我特意编写了以下的测试代码:

using System;

namespace StrTest
{
  class Program
  {
    //定义3个字符串 以便测试在多种情况下 下面6种判断方法的速度
    public static string strTest01 = "";
    public static string strTest02 = string.Empty;
    public static string strTest03 = "0123456789";

    public static DateTime start, end; //定义开始时间 和 结束时间
    public static TimeSpan ts;    //定义两个时间的间隔

    //**********************对strTest使用6种测试方法*****************************
    public static void Test(string strTest)
    {
      //string == ""
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (strTest == "")
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string == /"/" 时间消耗为 " + ts.TotalSeconds + " 秒");

      //string.Equals("")
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (strTest.Equals(""))
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string.Equals(/"/") 时间消耗为 " + ts.TotalSeconds + " 秒");

      //string == stirng.Empty
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (strTest == string.Empty)
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string == string.Empty 时间消耗为 " + ts.TotalSeconds + " 秒");

      //string.Equals(string.Empty)
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (strTest.Equals(string.Empty))
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string.Equals(string.Empty) 时间消耗为 " + ts.TotalSeconds + " 秒");

      //string.Length == 0
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (strTest.Length == 0)
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string.Length == 0 时间消耗为 " + ts.TotalSeconds + " 秒");

      //string.IsNullOrEmpty(string)
      start = DateTime.Now;
      for (int counter = 0; counter <= 100000000; counter++)
      {
        if (string.IsNullOrEmpty(strTest))
        {
        }
      }
      end = DateTime.Now;
      ts = end - start;
      Console.WriteLine("string.IsNullOrEmpty(string) 时间消耗为 " + ts.TotalSeconds + " 秒" + "/n");
    }

    static void Main(string[] args)
    {
      Console.WriteLine("=======================================");
      Console.WriteLine("strTest = /"/" 的5种测试结果");     
      Console.WriteLine("=======================================");

      Test(strTest01);

      Console.WriteLine("=======================================");
      Console.WriteLine("strTest = string.Emtpy 的5种测试结果"); 
      Console.WriteLine("=======================================");

      Test(strTest02);

      Console.WriteLine("=======================================");
      Console.WriteLine("strTest = /"0123456789/" 的5种测试结果"); 
      Console.WriteLine("=======================================");

      Test(strTest03);

      Console.ReadLine();  //等待键盘的输入 作用:使屏幕暂停在此处
    }
  }
}

我把能关的软件都关闭掉了  尽可能的屏蔽掉系统影响  并且让6种方法都运行了1亿次

第一次的截图:

第一次测试结果

第二次的截图: 

第二次测试结果

从以上可以看出:字符串在三种情况下,string.Length == 0的效率无疑是最高的。

总结

1. strTest== "" 不推荐使用,只能判断“值为空字符串”的字符串变量,而且效率比较低。

2. strTest.Equals("") 不推荐使用,同 1。

3. strTest== string.Empty 不推荐使用,只能判断“值为null”的字符串变量,而且效率低。

4. strTest.Equals(string.Empty) 不推荐使用,同 3。

5. strTest.Length == 0  这种方式,我不怎么喜欢用,不推荐使用。在自己的实际测试中,确实能证明这种判断方式的执行效率最高,但要使用它你必须保证字符串不null,如果为null就会报出异常。

6. string.IsNullOrEmpty(strTest)  这种方法是我最喜欢用的,它不但一次性能判断"空的字符串变量",还能判断“值为空字符串的变量”,并且还可以让代码简洁美观。判断的效率也不算低。

更多精彩内容其他人还在看

C#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多