判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?请问原因是什么??二叉数 binary tree 是一个二元组 BT=(D,R)
R表示结点之间关系的有限集合,D表示结点的有限集合。
请问:一个二元组怎么理解呀。
D,R各是数组,但D集合看成X, R集合看成是Y,这个二元组就是一个坐标呀 P(X,Y)这种理解对吗??
这是数组中的内容

解决方案 »

  1.   

    str.Length==0最快,因为整数判断最快,没有经过实例化等复杂的过程
      

  2.   

    TestCode
    static void juge()
            { 
                string str = "csdn";
                System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str == "")
                    { }
                }
                st.Stop();
                Console.WriteLine("str == '' : "+st.ElapsedMilliseconds);
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str == String.Empty)
                    { }
                }
                st.Stop();
                Console.WriteLine("str == String.Empty : " + st.ElapsedMilliseconds);
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str.Length == 0)
                    { }
                }
                st.Stop();
                Console.WriteLine("str.Length == 0 : " + st.ElapsedMilliseconds);
            }
    Result(str = "csnd"):
    str == '':      1292
    String.Empty:   2584
    str.Length:     2955
    Result : str = ""
    str == '':      676
    String.Empty:   2306
    str.Length:     2696结论:A最快。欢迎指正。
      

  3.   

    using System;
    class Test
    {
        static void Main()
        {
            string str = "csdn";
            System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str == "")
                { }
            }
            st.Stop();
            Console.WriteLine("str == '' : " + st.ElapsedMilliseconds);
            st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str == String.Empty)
                { }
            }
            st.Stop();
            Console.WriteLine("str == String.Empty : " + st.ElapsedMilliseconds);
            st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str.Length == 0)
                { }
            }
            st.Stop();
            Console.WriteLine("str.Length == 0 : " + st.ElapsedMilliseconds);
            Console.ReadKey();
        }
    }str == '' : 2984
    str == String.Empty : 2967
    str.Length == 0 : 426
      

  4.   

    清清月儿的结果:那么为什么if(a.Length==0)最快呢?
    因为整数判断等于最快,没有经过实例化等复杂的过程。具体看这里:http://blog.csdn.net/21aspnet/archive/2007/04/28/1588047.aspx和9楼结果不同
      

  5.   

    smntbk 的代码让我有收获!
      

  6.   

    if(str!=null && a.Trim().Length==0)
      

  7.   

    汗,还真是。
    在后两个st.Start()之前先复位 st.Reset();
    结果:
    str == '':      1289
    String.Empty:   1176
    str.Length:     365应该是判断Length最快的。
    改正!
      

  8.   


    ↑不好意思,以上答案是错误的,判定为空正确最快应该是方法C,如上面几楼所说,判定整型是最快的。关于这个实验这位弟兄你有没有发现每次最后那个值是最大的。其实st.ElapsedMilliseconds是在累加...如果对我说的有所怀疑,你可在相同系统运行环境下调试这段代码三次,只要你改变一下输出的先后顺序,你就会发现str.Length==0的判定时间大概只有另外两种的五分之一,甚至更少。
    以上所述本人也做过和你相近的测试,在本机上运行多次后方法A和B所花时间都在1000~1100左右,但是方法C的时间只有210~230。
    所有楼主的问题1答案应该为方法C。问题2本人只可以说,具体情况要具体分析,按楼主所说的按字面上理解是对的。
      

  9.   

    楼主的提法是否有问题?str是否为空应该用 str == null判断吧
      

  10.   

    楼主的提法是不是有点问题?引用类型的变量判断是否为空,应该用 == null判断吧
      

  11.   

    对于第一个问题有不同的看法,如果使用str.Length判断,当str=null时会异常
      

  12.   

    好像用IsNullOrEmpty是最安全稳定的
      

  13.   

     System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str==null || str == "")
                    { }
                }
                st.Stop();
               message += "\r\nstr == '' : " + st.ElapsedMilliseconds;
                st = new System.Diagnostics.Stopwatch();
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str == String.Empty)
                    { }
                }
                st.Stop();
                 message += "\r\nstr == String.Empty : " + st.ElapsedMilliseconds;
                st = new System.Diagnostics.Stopwatch();
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (str == null || str.Length == 0)
                    { }
                }
                st.Stop();
                message += "\r\nstr.Length == 0 : " + st.ElapsedMilliseconds;
                st = new System.Diagnostics.Stopwatch();
                st.Start();
                for (int i = 0; i < 100000000; i++)
                {
                    if (String.IsNullOrEmpty (str))
                    { }
                }
                st.Stop();
                message += "\r\nString.IsNullOrEmpty: " + st.ElapsedMilliseconds;            this.textBox2.Text = message;
    debug状态下:
    str = ""str == '' : 1343
    str == String.Empty : 2178
    str.Length == 0 : 719
    String.IsNullOrEmpty: 1127str = "adsad"str == '' : 1926
    str == String.Empty : 1907
    str.Length == 0 : 684
    String.IsNullOrEmpty: 1167str = nullstr == '' : 533
    str == String.Empty : 1453
    str.Length == 0 : 512
    String.IsNullOrEmpty: 922判断不为空:str = ""str == '' : 1433
    str == String.Empty : 2344
    str.Length == 0 : 694
    String.IsNullOrEmpty: 892str = "sfsf"
    str == '' : 2108
    str == String.Empty : 1999
    str.Length == 0 : 707
    String.IsNullOrEmpty: 845str = nullstr == '' : 507
    str == String.Empty : 1527
    str.Length == 0 : 489
    String.IsNullOrEmpty: 839当字符串特别长时:str == '' : 1998
    str == String.Empty : 1989
    str.Length == 0 : 701
    String.IsNullOrEmpty: 850应该是这种最快吧
    if (str != null && str.Length > 0)
    or if (str == null || str.Length == 0)
      

  14.   

    奇怪的是,我测试出来的
    if (!String.IsNullOrEmpty (str)) 比if (String.IsNullOrEmpty (str))
    快...
      

  15.   

    X是集合,比如(a,b,c,d)
    Y也是集合,比如(a@b,C@d)
    这个不能看成坐标吧
      

  16.   

    str.Length==0的速度最快,正如前面所说的,数字的比较比字符串的比较要快. 楼主所说的二叉树问题中的二元组就是数组啊,实际上说的是BT是一个二维数组,D表示二叉树的节点,R表示节点间的关系,比如父子关系
      

  17.   

    用b、str==String.Empty; 比较好
      

  18.   

    str.Length==0的速度最快,正如前面所说的,数字的比较比字符串的比较要快. 
      

  19.   


    确实需要改正,不然误导别人。每次st重新Start()之前,要先Reset()一下。你得出的时间后面的是前面的累积。