c# - C# 性能,加速类中的字符串数组
问题描述
我需要更多的性能,这就是我目前所拥有的。
public static class ValidateConditions
{
public static bool CheckWildCards(string hcdpPlnCvgCD, string HcdpPmtFctrCd, string hccCpmtFctrCd, IBizClaimWorkflowContext workspaceContext, string conditionType)
{
var patterns = new string[3];
if(condition)
{
patterns = new string[]{
hcdpPlnCvgCD,
"DEF",
"HIJ"
};
}
else
{
patterns = new string[]{
hcdpPlnCvgCD,
"yyy",
"zzz"
};
}
var matchResult = dataMart.SponsorPackage.Where(s => Regex.IsMatch(s.ExternalPolicy, pattern)).FirstOrDefault();
}
}
我听说这可能是一个性能问题,因为“这些数组应该声明为私有静态,因为我们只在类初始化时创建每个数组一次,而不是每次调用方法时创建一个新数组。因为它是每天调用一百万次。
所以我对如何实现这一点有点困惑。
我正在考虑在课堂上这样做, private static string[] patterns { get; set; }
但我最终还是不得不做 `new string[]{...} 那么我怎样才能真正正确地做到这一点?
更新:修复代码。
解决方案
只初始化一次变量。
public static class ValidateConditions
{
private static string[] Patterns1 = new string[]{
"ABC",
"DEF",
"HIJ"
};
private static string[] Patterns2 = new string[]{
"xxx",
"yyy",
"zzz"
};
public static bool CheckWildCards(MyClass data, bool condition)
{
var patterns = condition ? Patterns1 : Patterns2;
var matchResult = data.SponsorPackage.Where(s => MyMethod(s.ExternalPolicy, pattern)).FirstOrDefault();
//etc....
}
}
推荐阅读
- google-cloud-firestore - Swift/FireStore - 追加/更新数组中的元素
- python - 如何格式化字符串以创建可迭代列表?
- css - 即使使用全局运算符,也适用于纤细的组件样式
- python - 获取 TypeError:rndm_json_data_gen() 缺少 1 个必需的位置参数:'rangenum'
- c# - 将 BlazorWasm .NET 托管项目 Dockerize 到单个容器中?
- python - Python Google Cloud 函数缺少日志条目
- javascript - 离子选择不触发离子事件
- javascript - 反应最终形式错误:必须指定渲染道具
- python - 奇怪的 random.randint 行为
- terminal - 未找到 Command Brew?