什么是最有效的方式来确定未修剪的字符串在C#中是否为空? [英] What's the most efficient way to determine whether an untrimmed string is empty in C#?

查看:121
本文介绍了什么是最有效的方式来确定未修剪的字符串在C#中是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其周围可能有空白字符,我想检查它是否基本上是空的。



有很多方法这个:

  1 if(myString.Trim()。Length == 0)
2 if(myString.Trim ()==)
3 if(myString.Trim()。Equals())
4 if(myString.Trim()== String.Empty)
5 if (myString.Trim()。Equals(String.Empty))

通常是一个明显的过早优化的情况,但我很好奇,有一个机会,这将做足够的性能影响。



所以这是最有效的方法吗?



有没有更好的方法我没有想过?




编辑:访问此问题的访问者的注释:


  1. 对此问题进行了一些惊人的详细调查,特别是来自Andy和Jon Skeet。


  2. 你在搜索某个东西时就遇到了这个问题,所以你至少应该阅读完整的Andy和Jon的帖子。


看来有几个非常有效的方法,并且最高效的依赖于我需要处理的字符串的内容。



如果我不能预测字符串(我不能在我的情况下),Jon的 IsEmptyOrWhiteSpace 方法似乎一般更快。



感谢您的投入。我将选择Andy的答案作为正确的,因为他应该为他的努力的声誉提高,Jon已经有十几亿的声誉。

解决方案

编辑:新测试

 测试订单:
x。测试名称
小时:xxxxx //空字符串
小时:xxxxx //两个空格
小时:xxxxx //单字母
小时:xxxxx //带空格的单个字母
Ticks:xxxxx // long string
Ticks:xxxxx //带空格的长字符串

1. if(myString.Trim()。Length == 0)
ticks :4121800
ticks:7523992
ticks:17655496
ticks:29312608
ticks:17302880
ticks:38160224

2. if .Trim()==)
ticks:4862312
ticks:8436560
ticks:21833776
ticks:32822200
ticks:21655224
ticks: 42358016


3. if(myString.Trim()。Equals())
ticks:5358744
ticks:9336728
ticks:18807512
ticks:30340392
ticks:18598608
ticks:39978008


4. if(myString.Trim()== String.Empty)
ticks:4848368
ticks:8306312
ticks:21552736
ticks:32081168
ticks:21486048
ticks:41667608


5. if(myString.Trim()。Equals(String.Empty))
ticks:5372720
ticks:9263696
ticks:18677728
ticks:29634320
ticks:18551904
ticks:40183768


6. if(IsEmptyOrWhitespace(myString))//见John Skeet的Post算法
ticks:6597776
ticks:9988304
ticks:7855664
ticks:7826296
ticks:7885200
ticks:7872776

7. is(string.IsNullOrEmpty(myString。 Trim())// Cloud的建议
ticks:4302232
ticks:10200344
ticks:18425416
ticks:29490544
ticks:17800136
ticks:38161368

使用的代码:

  public void Main()
{

string res = string.Empty;

for(int j = 0; j <= 5; j ++){

string myString =;

switch(j){

case 0:
myString =;
break;
case 1:
myString =;
break;
case 2:
myString =x;
break;
case 3:
myString =x;
break;
case 4:
myString =这是一个长字符串,用于测试修剪空的东西。
break;
case 5:
myString =这是一个长字符串,用于测试修剪空的东西。

break;
}

bool result = false;
秒表sw = new Stopwatch();

sw.Start();
for(int i = 0; i <= 100000; i ++){


result = myString.Trim()。
}
sw.Stop()


res + =ticks:+ sw.ElapsedTicks + Environment.NewLine;
}


Console.ReadKey(); // break point here to get the results
}


I have a string that may have whitespace characters around it and I want to check to see whether it is essentially empty.

There are quite a few ways to do this:

1  if (myString.Trim().Length == 0)
2  if (myString.Trim() == "")
3  if (myString.Trim().Equals(""))
4  if (myString.Trim() == String.Empty)
5  if (myString.Trim().Equals(String.Empty))

I'm aware that this would usually be a clear case of premature optimization, but I'm curious and there's a chance that this will be done enough to have a performance impact.

So which of these is the most efficient method?

Are there any better methods I haven't thought of?


Edit: Notes for visitors to this question:

  1. There have been some amazingly detailed investigations into this question - particularly from Andy and Jon Skeet.

  2. If you've stumbled across the question while searching for something, it's well worth your while reading at least Andy's and Jon's posts in their entirety.

It seems that there are a few very efficient methods and the most efficient depends on the contents of the strings I need to deal with.

If I can't predict the strings (which I can't in my case), Jon's IsEmptyOrWhiteSpace methods seem to be faster generally.

Thanks all for your input. I'm going to select Andy's answer as the "correct" one simply because he deserves the reputation boost for the effort he put in and Jon has like eleventy-billion reputation already.

解决方案

Edit: New tests:

Test orders:
x. Test name
Ticks: xxxxx //Empty String
Ticks: xxxxx //two space
Ticks: xxxxx //single letter
Ticks: xxxxx //single letter with space
Ticks: xxxxx //long string
Ticks: xxxxx //long string  with space

1. if (myString.Trim().Length == 0)
ticks: 4121800
ticks: 7523992
ticks: 17655496
ticks: 29312608
ticks: 17302880
ticks: 38160224

2.  if (myString.Trim() == "")
ticks: 4862312
ticks: 8436560
ticks: 21833776
ticks: 32822200
ticks: 21655224
ticks: 42358016


3.  if (myString.Trim().Equals(""))
ticks: 5358744
ticks: 9336728
ticks: 18807512
ticks: 30340392
ticks: 18598608
ticks: 39978008


4.  if (myString.Trim() == String.Empty)
ticks: 4848368
ticks: 8306312
ticks: 21552736
ticks: 32081168
ticks: 21486048
ticks: 41667608


5.  if (myString.Trim().Equals(String.Empty))
ticks: 5372720
ticks: 9263696
ticks: 18677728
ticks: 29634320
ticks: 18551904
ticks: 40183768


6.  if (IsEmptyOrWhitespace(myString))  //See John Skeet's Post for algorithm
ticks: 6597776
ticks: 9988304
ticks: 7855664
ticks: 7826296
ticks: 7885200
ticks: 7872776

7. is (string.IsNullOrEmpty(myString.Trim())  //Cloud's suggestion
ticks: 4302232
ticks: 10200344
ticks: 18425416
ticks: 29490544
ticks: 17800136
ticks: 38161368

And the code used:

public void Main()
{

    string res = string.Empty;

    for (int j = 0; j <= 5; j++) {

        string myString = "";

        switch (j) {

            case 0:
                myString = "";
                break;
            case 1:
                myString = "  ";
                break;
            case 2:
                myString = "x";
                break;
            case 3:
                myString = "x ";
                break;
            case 4:
                myString = "this is a long string for testing triming empty things.";
                break;
            case 5:
                myString = "this is a long string for testing triming empty things. ";

                break;
        }

        bool result = false;
        Stopwatch sw = new Stopwatch();

        sw.Start();
        for (int i = 0; i <= 100000; i++) {


            result = myString.Trim().Length == 0;
        }
        sw.Stop();


        res += "ticks: " + sw.ElapsedTicks + Environment.NewLine;
    }


    Console.ReadKey();  //break point here to get the results
}

这篇关于什么是最有效的方式来确定未修剪的字符串在C#中是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆