【问题标题】:Quickest way to filter a list of words过滤单词列表的最快方法
【发布时间】:2012-06-28 01:14:17
【问题描述】:

我有一个包含几个单词的列表。我想过滤掉其中一些与特定模式不匹配的。将所有匹配项添加到临时列表并随后将此列表复制到主列表中是否更快?或者从主列表中删除所有不匹配是否更快? 我要尽可能快地过滤10000个单词,所以我很期待每一个小小的速度提升。

编辑:

string characters = "aAbBcC";
// currentMatches contains all the words from the beginning
List<string> currentMatches = new List<string>();
List<string> newMatches = new List<string>();
foreach (string word in currentMatches)
{
   if (characters.IndexOf(word[0]) > -1)
   // word match
   {
       newMatches.Add(word);
   }
}
currentMatches = newMatches;

foreach 循环应检查word 是否以characters 的字符之一开头。在这里,我将所有匹配项复制到newMatches,然后将所有新匹配项复制到currentMatches。

【问题讨论】:

  • 没有。它必须非常快,而且我不知道我的 CPU 内部会发生什么。所以我不知道什么更快。
  • 也许你应该尝试一些方法——我建议使用 Linq,因为它非常快——比如 yourList.Where(x => x == someFilterExpression)。您是否正在优化某些东西 - 您是否有一些已经很慢但您正在尝试优化的东西?需要闪电般快速的实际原因是什么?
  • 10000 字不算多。我怀疑你会用任何方法得到显着的改进。
  • @Tudor 是对的。只是为了给您一些观点,如果您有一个包含 10,000 个条目的 Dictionary,则在我没什么特别的笔记本电脑上运行的该 Dictionary 上执行 1,000,000 次 TryGet 查找所需的时间不到 100 毫秒。

标签: c# list filter


【解决方案1】:

假设List&lt;T&gt;,那么您必须考虑以下几点:

  • 如果 Count 小于 Capacity,则 Add 方法是 O(1) 操作。如果需要增加容量来容纳新元素,这个方法就变成了O(n)操作,其中n是Count;
  • RemoveAt 方法是一个 O(n) 操作,其中 n 是(计数 - 索引)。

如果您创建列表以保存初始容量设置为总字数的匹配项,那么Add 将始终为 O(1) 并且更快。但是,您需要考虑创建这个容量设置为总字数的新列表的开销。

归根结底,您需要对其进行测试,看看哪种方法更适合您的特定场景。

【讨论】:

  • 最后一行:像一个真正的政治家一样说话:D
【解决方案2】:

以下是我汇总的关于如何计时方法的示例。有很多方法可以做到这一点,我认为你将不得不尝试一些。您可以使用 João Angelo 的帖子中的信息来帮助您找到好的方法,但这里有一些。此外,如果您愿意花时间,您可以将这一切放在一个循环中,该循环将创建一个新列表,运行所有测试,将 TimeSpan 结果放入集合而不是 Console.WriteLine'ing 它们,然后然后给你一个平均的测试迭代次数。这将有助于给你一个平均值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<string> testList = CreateTestList();
            const string filter = "abc";

            TimeNewListMethod(FilterIntoNewListWithLinq, testList, filter);
            TimeInPlaceMethod(FilterInPlaceWithLinq, testList, filter);

            TimeNewListMethod(FilterIntoNewListWithForEach, testList, filter);

            TimeInPlaceMethod(FilterInPlaceWithRemoveAll, testList, filter);

            Console.Read();
        }

        public static void TimeInPlaceMethod(Action<List<string>, string> testMethod, List<string> toFilter, string filter)
        {
            List<string> toFilterCopy = new List<string>(toFilter);
            DateTime time = DateTime.Now;
            testMethod(toFilterCopy, filter);
            Console.WriteLine(DateTime.Now - time);
        }

        public static void TimeNewListMethod(Func<List<string>, string, List<string>> testMethod, List<string> toFilter, string filter)
        {
            List<string> toFilterCopy = new List<string>(toFilter);
            List<string> resultList;
            DateTime time = DateTime.Now;
            resultList = testMethod(toFilterCopy, filter);
            Console.WriteLine(DateTime.Now - time);
        }

        public static List<string> FilterIntoNewListWithLinq(List<string> toFilter, string filter)
        {
            return toFilter.Where(element => element.IndexOf(filter) > -1).ToList();
        }

        public static void FilterInPlaceWithLinq(List<string> toFilter, string filter)
        {
            toFilter = toFilter.Where(element => element.IndexOf(filter) > -1).ToList();
        }

        public static List<string> FilterIntoNewListWithForEach(List<string> toFilter, string filter)
        {
            List<string> returnList = new List<string>(toFilter.Count);
            foreach (string word in toFilter)
            {
                if (word.IndexOf(word[0]) > -1)
                {
                    returnList.Add(word);
                }
            }

            return returnList;
        }

        public static void FilterInPlaceWithRemoveAll(List<string> toFilter, string filter)
        {
            toFilter.RemoveAll(element => element.IndexOf(filter) == -1);
        }

        public static List<string> CreateTestList(int elements = 10000, int wordLength = 6)
        {
            List<string> returnList = new List<string>();
            StringBuilder nextWord = new StringBuilder();

            for (int i = 0; i < elements; i++)
            {
                for (int j = 0; j < wordLength; j++)
                {
                    nextWord.Append(RandomCharacter());
                }
                returnList.Add(nextWord.ToString());
                nextWord.Clear();
            }

            return returnList;
        }

        public static char RandomCharacter()
        {
            return (char)('a' + rand.Next(0, 25));
        }

        public static Random rand = new Random();
    }
}

【讨论】:

    【解决方案3】:

    整体

    characters.IndexOf(word[0]) > -1

    对我来说有点陌生,所以我会为未来的程序员寻找更具可读性和可维护性的东西。我花了一分钟才弄清楚您正在检查列表中每个字符串中的第一个字符,以寻找 { a A, B, C, a, b, c } 范围内的匹配项。它有效,但对我来说,它有点神秘。我开始花时间阅读它,但我会这样做:

            foreach (string word in currentMatches)
            {               
                if (Regex.IsMatch(word, "^([A-Ca-c])"))
                {
                    newMatches.Add(word);
                }
            }
    

    我不会担心将临时列表复制回初始列表。你已经定义它填充它,继续使用它。

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2015-07-22
      • 1970-01-01
      • 2016-12-29
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多