2013.07.08摘录

C#中“Equals”与“==”的速度比较

时间:2010-03-22 09:09来源:未知 作者:admin

最初接触C#时就一直疑惑,比较字符串的方法:“Equals”与“==”在比较时性能有什么区别呢?相信这个问题也曾困惑着许多人,那么我们就一起来探讨下:

比较一个string是否等于一个值,最初接触C#的人会这么写:

string myStr = "value";
  
if (myStr.Equals("value"))
{
  
}
  
if (myStr == "value")
{
      
}

然后,会有人笑你很菜,得知把要判断的value写在前面性能高些,要这样写:

string myStr = "value";
  
if ("value".Equals(myStr))
{
  
}
  
if ("value" == myStr)
{
      
}

但或许他也只是从那个笑他很菜的前辈那里得知应该这样写,而正确与否、差别,没有真正自己去测试过。这里先不告诉大家上面“前辈”说的正确与否,我们来测试下:

public class Program
{
    public static void Main(string[] args)
    {
        int time = 100000000;
        int start, end, i;
        bool result;
        string myStr = "that's funny";
  
        Console.Title = "Equals & == test";
  
        ////////////////////////////////////////////////////////////////
        // True test 1 - string.Equals("value")
        ////////////////////////////////////////////////////////////////
  
        // 获取程序开始后经过的毫秒数
        start = System.Environment.TickCount;
  
        // 判断一次是否相等几乎不需要时间,所以我们让计算机判断一亿次,
        // 这真是个天文数字,囧
        for (i = 0; i < time; i++)
        {
            result = myStr.Equals("that's funny");
        }
          
        // 这里得到的就是用Equals方法时距离程序启动的毫秒数
        // 因此,只要用end减去start除以1000便是Equals方法所花费的秒数
        end = System.Environment.TickCount;
        Console.WriteLine("True test 1 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // True test 2 - "value".Equals(string)
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = "that's funny".Equals(myStr);
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("True test 2 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // True test 3 - string == "value"
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = myStr == "that's funny";
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("True test 3 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // True test 4 - "value" == string
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = "that's funny" == myStr;
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("True test 4 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // False test 1 - string.Equals("value")
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = myStr.Equals("that's fun");
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("False test 1 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // False test 2 - "value".Equals(string)
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = "that's fun".Equals(myStr);
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("False test 2 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // False test 3 - string == "value"
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = myStr == "that's fun";
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("False test 3 cost time: " + (end - start) / 1000.0 + " s");
  
        ////////////////////////////////////////////////////////////////
        // False test 4 - "value" == string
        ////////////////////////////////////////////////////////////////
  
        start = System.Environment.TickCount;
  
        for (i = 0; i < time; i++)
        {
            result = "that's fun" == myStr;
        }
  
        end = System.Environment.TickCount;
        Console.WriteLine("False test 4 cost time: " + (end - start) / 1000.0 + " s");
  
        Console.ReadLine();
    }
}

上面通过8个小测试,逐一计算了各种写法在当判断结果为true与false情况下花费的时间,下面是运行5次所得到的结果:

2010-03-21 Sunday 007

下面列出5次测试结果的对比:

 

  true测试最快方式 true测试最快方式与第二名的差距 false测试最快方式 false测试最快方式与第二名的差距
测试1 "value" == string 0.046 s "value".Equals(string) 0.016 s
测试2 "value" == string 0.219 s "value".Equals(string) 0.036 s
测试3 string == "value" 0.015 s "value".Equals(string) 0.001 s
测试4 string == "value" 0.068 s string.Equals("value") 0.021 s
测试5 string == "value" 0.066 s "value".Equals(string) 0.001 s

 

 

true测试:"value" == string领先总时间为0.265秒,string == "value"领先总时间为0.149秒;

false测试:"value".Equals(string)领先总时间为0.054秒,string.Equals("value")领先总时间为0.021秒。

 

现在我们暂时可以得到的结论是:true判断时,用"value" == string是最快的;false判断时,用"value".Equals(string)是最快的。

也就是说:一个判断true发生的情况多些时,用"value" == string;false发生的情况多些时,用"value".Equals(string)。

以上内容完全摘录于网上,仅供自己或他人学习使用,如有侵权,请及时告知本人。

原文地址:https://www.cnblogs.com/CuiPengfei19911014/p/3178697.html