簡體   English   中英

在C#中,刪除字符串[]或List中空白條目的最快方法是什么 <string> ?

[英]In C#, what is the fastest way to remove blank entries in a string[] or List<string>?

我有非常大的字符串列表和數組,我發現了2個我想要解決的問題:

  1. 刪除所有空字符串條目
  2. 刪除所有只是空格的條目

這些可以是2種不同的解決方案 不確定與基本循環或此相比是否有更快的方法:

array = array.Where(r=>!String.IsNullOrEmpty(r.Trim());

對於List<T>有一個可能更快的等價物,它就地執行刪除, RemoveAll

// We can do better than this - see below...
list.RemoveAll(r => String.IsNullOrEmpty(r.Trim()));

就如何執行列表中的重新定位而言,這可能更快。

當然,這還取決於你是否想要就地刪除。 我個人通常更喜歡LINQ方法,因為它現在更靈活,更慣用 - 我通常集合視為不可變序列,即使它們不是真的:)

需要注意的一點是:您不需要修剪字符串以查明它是否有任何空格。 你可以使用string.IsNullOrWhiteSpace ,它應該被稱為IsNullOrEmptyOrWhitespace - 基本上“它沒有內容”。

這很容易對性能產生非常顯着的影響 - 如果你有很多長字符串,那么為了確定有一些內容而沒有必要進行O(N)操作( Trim )......並且沒有必要創建一個新的字符串,當你再次扔掉它時。

注意:尺寸從早期版本改變,以便在最后三個案例之間進行良好區分......

這是一個例子:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

public class Test
{
    static void Main()
    {
        List<string> list = new List<string>();
        string longString = new string('x', 1000) + " ";
        for (int i = 0; i < 1000000; i++)
        {
            list.Add(i % 100 == 0 ? "" : longString);
        }

        Stopwatch sw = Stopwatch.StartNew();
        list.Where(r=> !string.IsNullOrEmpty(r.Trim())).ToList();
        sw.Stop();
        Console.WriteLine("IsNullOrEmpty(Trim): {0}", sw.ElapsedMilliseconds);

        GC.Collect();

        sw = Stopwatch.StartNew();
        list.Where(r=> !string.IsNullOrWhiteSpace(r)).ToList();
        sw.Stop();
        Console.WriteLine("IsNullOrWhitespace: {0}", sw.ElapsedMilliseconds);

        GC.Collect();

        sw = Stopwatch.StartNew();
        List<string> listResult = new List<string>();
        int countList = list.Count;
        for (int i = 0; i < countList; i++)
        {
            string item = list[i];
            if (!string.IsNullOrWhiteSpace(item))
            {
                listResult.Add(item);
            }
        }
        sw.Stop();
        Console.WriteLine("New list: {0}", sw.ElapsedMilliseconds);

        GC.Collect();

        // This has to be last, as it modifies in-place 
        sw = Stopwatch.StartNew();
        list.RemoveAll(r => string.IsNullOrWhiteSpace(r));
        sw.Stop();
        Console.WriteLine("List.RemoveAll: {0}", sw.ElapsedMilliseconds);
    }        
}

我的筆記本上的示例結果:

IsNullOrEmpty(Trim): 3573
IsNullOrWhitespace: 452
New list: 232
List.RemoveAll: 153

for/foreach Linq在場景后面使用for/ForEach ,經典for/foreach總是比任何Linq到對象表達式更快。

我唯一想到的是使用IsNullOrWhitespace方法。

array = array.Where(r=>!String.IsNullOrWhitespace(r));

我懷疑你會注意到任何速度差異。 這是微優化的一個極端例子。

您可以使用

array = array.Where(r=>!String.IsNullOrWhiteSpace(r));

如果你有一個List<string>

list.RemoveAll(str => string.IsNullOrWhiteSpace(str));

如預期的那樣經典for快那么任何LINQ解決方案

static void Main(string[] args)
    {


        List<string> list = new List<string>();
        string longString = new string('x', 10000) + " ";
        for (int i = 0; i < 1000000; i++)
        {
            list.Add(i % 100 == 0 ? "" : longString);
        }

        Stopwatch sw = Stopwatch.StartNew();
        list.Where(r => !string.IsNullOrEmpty(r.Trim())).ToList();
        sw.Stop();
        Console.WriteLine("IsNullOrEmpty(Trim): {0}", sw.ElapsedMilliseconds);

        GC.Collect();

        sw = Stopwatch.StartNew();
        list.Where(r => !string.IsNullOrWhiteSpace(r)).ToList();
        sw.Stop();
        Console.WriteLine("IsNullOrWhitespace: {0}", sw.ElapsedMilliseconds);


        GC.Collect();


        sw = Stopwatch.StartNew();
        //this is result list
        List<string> listResult = new List<string>();
        int countList = list.Count;
        for (int i = 0; i < countList; i++)
        {
            string item = list[i];
            if (!string.IsNullOrWhiteSpace(item))
            {
                listResult.Add(item);
            }
        }
        sw.Stop();
        Console.WriteLine("Classic for + IsNullOrWhitespace: {0}", sw.ElapsedMilliseconds);

        GC.Collect();

        sw = Stopwatch.StartNew();
        list.AsParallel().Where(r => !string.IsNullOrWhiteSpace(r)).ToList();
        sw.Stop();
        Console.WriteLine("PLINQ IsNullOrWhitespace: {0}", sw.ElapsedMilliseconds);

        Console.Read();
    }

在我的機器上(i5)

IsNullOrEmpty(Trim): 6165
IsNullOrWhitespace: 39
Classic for + IsNullOrWhitespace 21
PLINQ IsNullOrWhitespace: 65

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM