Sen*_*kin 4 c# performance dictionary
是否有更优的方法来执行以下操作 -
int interactionScore = 0;
foreach (var completionResult in needs.Keys.Intersect(results.Keys))
{
interactionScore -= results[completionResult] * needs[completionResult];
}
Run Code Online (Sandbox Code Playgroud)
需求和结果都是小词典(每个大约 2 - 10 个条目),但是我运行这个循环的次数非常高,这损害了我的性能,所以我想知道是否有更有效的方法来实现类似的结果(仅将两个字典中都存在的条目相乘)。
避免 Intersect
int interactionScore = 0;
foreach (var key in needs.Keys)
{
if (results.TryGetValue(key, out int result))
{
int need = needs[key];
interactionScore -= result * need;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |