I have the code below that gets hit several hundred times per second. I'm wondering what I can do to improve performance. It seems that there should be some way to build up a substring index. Is there some existing examples of that or an explanation on how to do this?
using System;
using System.Collections.Generic;
using System.Linq;
using Asi.Server.Interfaces.History;
namespace Asi.Server.History.AssetHistory
{
public class TelemetrySubscriptionFilter: ITelemetryLogFilter, ITelemetryLogAutosubscriber
{
private readonly List<string> _shouldNotLogs = new List<string>
{
"Local Position Service",
"Global Position Service",
"PositionGroup.",
"DKS/"
};
public bool ShouldNotLog(string telemetryName)
{
float period;
if (ShouldAutoSubscribe(telemetryName, out period))
return false;
// telemetryName is longer than our filter texts
return _shouldNotLogs.Any(s => telemetryName.IndexOf(s, StringComparison.Ordinal) >= 0);
}
private readonly List<string> _shouldAutoSubscribe = new List<string>
{
"Manual Mode",
"Autonomous Driven",
"Battery Voltage",
"RPM",
"Engine On",
"Parking Brake",
"Gear",
"Stopping Distance",
"Ready For Motion",
"GPS Correction Sent",
"Setpoint",
"Dead Reckon",
"VCU uC",
"Feedback",
"Off Path",
"RMS",
"Yaw Rate",
"Stop Enabled",
"Arbiter",
"Velocity Error",
"Processor"
};
public bool ShouldAutoSubscribe(string telemetryName, out float period)
{
period = 0.375f; // chosen somewhat arbitrarily
return _shouldAutoSubscribe.Any(s => telemetryName.IndexOf(s, StringComparison.Ordinal) >= 0);
}
}
}
static
, but that won't affect the speed. \$\endgroup\$String.Contains()
? It already performs an ordinal comparison. \$\endgroup\$telemetryName
. \$\endgroup\$