从列表中到处删除具有特定值的元素的最有效方法?C#

Per*_*ing 4 c# loops list

编辑:本问题底部发布的不同技术的基准。

我有一个非常大的List<int>整数。我想从 .txt 文件中删除所有出现的“3” List<int>。哪种技术最有效地做到这一点?我通常会使用.Remove(3)扩展直到它返回false,但我担心每次内部调用都会不必要地.Remove(3)循环整个过程List<int>

编辑:评论中建议尝试

TheList = TheList.Where(x => x != 3).ToList();

我需要删除元素而不实例化新的 List

var TheList = new List<int> { 5, 7, 8, 2, 8, 3, 1, 0, 6, 3, 9, 3, 5, 2, 7, 9, 3, 5, 5, 1, 0, 4, 5, 3, 5, 8, 2, 3 };

//technique 1
//this technique has the shortest amount of code,
//but I fear that every time the Remove() method is called,
//the entire list is internally looped over again starting at index 0

while (TheList.Remove(3)) { }

//technique 2
//this technique is an attempt to keep the keep the list from
//being looped over every time an element is removed

for (var i = 0; i < TheList.Count; i++)
{
    if (TheList[i] == 3)
    {
        TheList.RemoveAt(i);
        i--;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来做到这一点?

基准测试

我测试了三种技术,从包含 100,000 个元素的数组中删除 10,138:上面显示的两种技术,以及 Serg 在答案中推荐的一种技术。结果如下:

  1. ‘while’循环:179.6808ms
  2. “for”循环:65.5099ms
  3. “RemoveAll”谓词:0.5982ms

在此输入图像描述

基准代码:

var RNG = new Random();
//inclusive min and max random number
Func<int, int, int> RandomInt = delegate (int min, int max) { return RNG.Next(min - 1, max) + 1; };

var TheList = new List<int>();
var ThreeCount = 0;
for (var i = 0; i < 100000; i++)
{
    var TheInteger = RandomInt(0, 9);
    if (TheInteger == 3) { ThreeCount++; }
    TheList.Add(TheInteger);
}
var Technique1List = TheList.ToList();
var Technique2List = TheList.ToList();
var Technique3List = TheList.ToList();
<div style="background-color:aquamarine;color:#000000;">Time to remove @ThreeCount items</div>

//technique 1
var Technique1Stopwatch = Stopwatch.StartNew();
while (Technique1List.Remove(3)) { }
var Technique1Time = Technique1Stopwatch.Elapsed.TotalMilliseconds;
<div style="background-color:#ffffff;color:#000000;">Technique 1: @(Technique1Time)ms ('while' loop)</div>

//technique 2
var Technique2Stopwatch = Stopwatch.StartNew();
for (var i = 0; i < Technique2List.Count; i++)
{
    if (Technique2List[i] == 3)
    {
        Technique2List.RemoveAt(i);
        i--;
    }
}
var Technique2Time = Technique2Stopwatch.Elapsed.TotalMilliseconds;
<div style="background-color:#ffffff;color:#000000;">Technique 2: @(Technique2Time)ms ('for' loop)</div>

//technique 3
var Technique3Stopwatch = Stopwatch.StartNew();
var RemovedCount = Technique3List.RemoveAll(x => x == 3);
var Technique3Time = Technique3Stopwatch.Elapsed.TotalMilliseconds;
<div style="background-color:#ffffff;color:#000000;">Technique 3: @(Technique3Time)ms ('RemoveAll' predicate)</div>
Run Code Online (Sandbox Code Playgroud)

Ser*_*erg 5

您可以使用List<T>.RemoveAll并传递您的谓词 - https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=net-6.0#System_Collections_Generic_List_1_RemoveAll_System_Predicate__0__ _ 。这保证了线性复杂度O(list.Count)

TheList.RemoveAll(x=>x==3);
Run Code Online (Sandbox Code Playgroud)

此外,RemoveAll在内部执行一些 GC 特定的事情,所以我认为在某些情况下,相对于简单的手工循环实现,这可能会提供一些额外的性能优势(但我在这里不确定)。

RemoveAll 如果您想自己完成这一切,您可以查看此处的实现。一般来说,它只是一个while循环,就像你的问题一样。

此外,正如我们从 GitHub 实现中看到的那样(正如 Jon Skeet 在评论中提到的),删除操作会导致列表的其余部分(第一个删除的项目之后的所有项目)在可用空间上复制(移动),这是由删除引起的。因此,如果您有非常大的列表和/或想要频繁删除某些内容,您可以考虑切换到其他数据结构,例如链表。