I have a lookup like ths:
Lookup<String, pages> plsBase = (Lookup<String, pages>)(Query<pages>($@"Select ...").ToLookup(s => s.ip, o => o));
It is very fast when I access it by key, but the problem is that I need to access it with StartsWith(). When I use StartsWith() like below, the performance is comparable to a regular List
var pls = plsBase.Where(x => (x.Key.StartsWith(classBIp, StringComparison.Ordinal))).SelectMany(x => x).ToList();
The question is what can be done to improve the performance when using StartsWith()?
This answer assumes that classBIp
is of a fixed length.
Option 1: Intermediate lookup [UPDATED]
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"));
}
}
}
Option 2: Compare a substring
var bipLength = classBip.Length;
var pls = plsBase.Where(x =>
(x.Key
.Substring(0, bipLength)
.Equals(classBIp, StringComparison.Ordinal)))
.SelectMany(x => x)
.ToList();
You might want to time both options to see which one performs better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.