• 关于字符串为空的几个性能测试


    http://www.cnblogs.com/anlyren/archive/2007/07/27/834148.html

    今天突然有兴做了两下有关字符串为空的性能测试,与大家分享!结果如下:
    两种赋值方式的比较:
    string str="";
    string str=string.Empty;
    理论上讲:
    string.Empty是一个Static的属性,使用时不分配存储空间,而在用""时,系统会分配一个长度为空的存储空间。不过编译系统应该会优化,也就是说,比如你程序中有10个地方用到了"",但好的编译系统应该引用的是同一个对象。所以用""也就是浪费一个对象空间而已。
    实战:
    测试程序如下:
    namespace testEmpty
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                test.testEmpty();
                test.testEqualEmpty();
                Console.Read();
            }        
        }
        class Test
        {
            public void testEmpty()
            {
                string str;
                for (int i = 0; i < 10000; i++)
                {
                    str = "";
                }
            }
            public void testEqualEmpty()
            {
                string str;
                for (int i = 0; i < 10000; i++)
                {
                    str = string.Empty;
                }
            }
        }
    }

     测试过程是分别将赋值语句str=""和str=string.Empty用两个函数执行10000次,所用时间如下所示:
        两个方法耗费时间表
    所以说:单独执行testEmpty()执行10000次用了0.262669毫秒,单独执行testEqualEmpty()执行0.026849毫秒。前者是后者的10倍.

    下面介绍的是几种判断语句的比较:
    我想到的所有的判断空字符串的语句就这几种了,大家还有其它方法的欢迎讨论!
    str == ""
    str.Equals("")
    str==string.Empty
    str.Equals(string.Empty)
    str .Length==0


    测试程序如下:
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace testEmpty
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                test.test1();
                test.test2();
                test.test3();
                test.test4();
                test.test5();
                Console.Read();
            }        
        }
        class Test
        {
            string str = string.Empty;
            public void test1()
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (str == "")
                    {
                        Console.WriteLine("1 This string is emput");
                    }
                }
            }
            public void test2()
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (str.Equals(""))
                    {
                        Console.WriteLine("2 This string is emput");
                    }
                }
            }
            public void test3()
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (str==string.Empty)
                    {
                        Console.WriteLine("3 This string is emput");
                    }
                }
            }
            public void test4()
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (str.Equals(string.Empty))
                    {
                        Console.WriteLine("4 This string is emput");
                    }
                }
            }
            public void test5()
            {
                for (int i = 0; i < 10000; i++)
                {
                    if (str .Length==0)
                    {
                        Console.WriteLine("5 This string is emput");
                    }
                }
            }
        }
    }

    在这个测试程序中,用了5个分别含有这5种判断语句的方法,目的就是为了测试每个方法耗费的时间。
    在这里说明一下,笔者在这个程序中起的名字不可取,程序员不应该这样为方法起名字的,见笑了!
    测试结果如下:
    判断5种语句的测试结果
     呵呵,可以从这个方法耗费时间详细说明表中看出,这些方法耗费时间都比较,这主要是因为里面的Console.WriteLine()语句影响的。但是每个方法中都有这一语句,所以说它并不影响我们的比较结果!
    得出的结论:在字符串为空时,这五种判断语句的耗费时间由短到长
    str .Length==0
    str.Equals("")
    str==string.Empty
    str.Equals(string.Empty)
    str == ""
    你平时有的哪种比较语句呢?呵呵……
    需要说明的是:这只是在字符串为空时结果是这样的,那么字符串不为空时呢,结果又是怎样的呢?

  • 相关阅读:
    iOS Provision 要点记录
    (FIFO)有名管道在无亲缘进程间的通信
    uuid Makefile share
    浅谈C语言中的联合体
    消息队列 进程通信
    onvif makefile without share
    共享内存 进程通信
    共享内存(非map) 进程通信
    消息队列 两个进程
    linux 进程通信
  • 原文地址:https://www.cnblogs.com/xiaoL/p/2068612.html
Copyright © 2020-2023  润新知