如何在使用StartsWith()时提高查找性能

3

我有一个像这样的查找:

   Lookup<String, pages>  plsBase = (Lookup<String, pages>)(Query<pages>($@"Select ...").ToLookup(s => s.ip, o => o));

通过 key 访问速度非常快,但问题在于我需要使用 StartsWith() 进行访问。当我像下面这样使用 StartsWith() 时,性能与普通的 List 相当。

var pls = plsBase.Where(x => (x.Key.StartsWith(classBIp, StringComparison.Ordinal))).SelectMany(x => x).ToList();

问题在于如何提高使用StartsWith()时的性能?


如果classBIp始终具有相同的长度,您可以使用.Equals比较子字符串,或创建一个单独的查找字典来处理其起始部分。 - Markus Deibel
@MarkusDeibel 这实际上是个很好的想法。 - realPro
@Enigmativity 当然也包括IPv6。 - realPro
@realPro - 这使得问题有点难度。你如何查询IPv6? - Enigmativity
@realPro - 这个掩码需要多久更改一次?创建查找表的工作量很大,但如果在此之后访问多次,则读取速度很快。如果您只进行几次查找,那么最好只是进行普通搜索。这就是我试图确定的。 - Enigmativity
显示剩余2条评论
1个回答

1
这个答案假定classBIp的长度是固定的。
选项1:中间查找[已更新]。
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace LookupTest
{
    public class Tests
    {
        [Test]
        public void IntermediateLookupTest()
        {
            var pageInfos = new Dictionary<string, string>
            {
                { "Home", "home.html" },
                { "About", "about.html" },
                { "Fineprint", "fineprint.html" },
                { "Finish", "finish.html" },
                { "Above", "above.html" }
            };

            // Corresponds to OP: plsBase = (Lookup<String, pages>)(Query<pages>($@"Select ...").ToLookup(s => s.ip, o => o));
            Lookup<string, string> plsBase = (Lookup<string, string>)pageInfos.ToLookup(k => k.Key, v => v.Value);

            Lookup<string, string> intermediateLookup = (Lookup<string, string>)pageInfos.ToLookup(k => k.Key.Substring(0, 3), v => v.Key);

            var classBIp = "Abo";

            var result = new List<string>();

            foreach (var plsBaseKey in intermediateLookup[classBIp])
            {
                result.AddRange(plsBase[plsBaseKey]);
            }

            Assert.AreEqual(2, result.Count);
            Assert.True(result.Contains("about.html"));
            Assert.True(result.Contains("above.html"));
        }
    }
}

选项2:比较子字符串。
var bipLength = classBip.Length;
var pls = plsBase.Where(x => 
    (x.Key
        .Substring(0, bipLength)
            .Equals(classBIp, StringComparison.Ordinal)))
    .SelectMany(x => x)
    .ToList();

你可能想测试两个选项的时间,以确定哪一个表现更好。

4
避免使用 .Where,因为这会抵消使用 Lookup / Dictionary 的好处,因为你强制它迭代整个数据结构。 - mjwills
我已经完全更新了选项1的答案。 .Where 已被删除,现在是可运行的测试。 - Markus Deibel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接