使用多个正则表达式命令(总共大约 20 个)解析消息时,我遇到了性能问题:
为了提高效率,我有:
1) 按可能性对正则表达式命令进行排序。
2)确保一旦找到匹配项,我就会跳出匹配循环。
我想知道我是否可以进行任何其他改进,或者是否有更好的方法来解决我的问题。
调用代码:
bool resolved = false;
Match regexMatch = null;
foreach (var resolverKvp in _resolvers)
{
if (resolverKvp.Key.Pattern.IsMatch(topicName))
{
regexMatch = resolverKvp.Key.Pattern.Match(topicName);
// Use the kvp value
resolved = true;
break;
}
}
迭代的正则表达式命令示例:
<add messagename="BackLayVolumeCurrencyOddsFormat" pattern="^.*/M/E_([0-9]+)/MEI/MDP/(\d{1,3})_(\d{1,3})_(\d+)_([a-zA-Z]{3})_([1-3])$" assembly="Client.Messaging"
type="Client.Messaging.TopicMessages.BackLayVolumeCurrencyOddsFormatResolver">
</add>
<add messagename="Market1" pattern="^.*/M/E_([0-9]+)$" assembly="Client.Messaging"
type="Client.Messaging.TopicMessages.Market1Resolver">
</add>
数据示例:
regex 1:
6/E/E_1/E/E_511010/E/E_527901/E/E_631809/E/E_631810/E/E_631811/M/E_1379656/MEI/MDP/10_10_1_USD_3
regex 2:
1/E/E_1/E/E_100004/E/E_190539/E/E_632113/E/E_632120/M/E_1380084
先感谢您。